<html><head></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; "><div><br><blockquote type="cite"><div><blockquote type="cite"><blockquote type="cite">In terms of safe publishing, you can consider this safe since in the start method of the ComponentRegistry, internal volatile fields (including some locks) are touched, which means a JMM fence [1] is crossed. &nbsp;<br></blockquote></blockquote><blockquote type="cite">Afaik safe publishing would only happen if *same* variables are touched by the user's thread accessing the object, is that the case?<br></blockquote><br>Piggybacking is a valid strategy and is used in some core JDK constructs including AbstractQueuedSynchronizer, which is used to build locks, etc. &nbsp;See Java Concurrency in Practice, section 16.1.4:<br><br><span class="Apple-tab-span" style="white-space:pre">        </span><a href="http://codeidol.com/java/java-concurrency/The-Java-Memory-Model/What-is-a-Memory-Model,-and-Why-would-I-Want-One/">http://codeidol.com/java/java-concurrency/The-Java-Memory-Model/What-is-a-Memory-Model,-and-Why-would-I-Want-One/</a><br><br>More fun reading: <br><br><span class="Apple-tab-span" style="white-space:pre">        </span>http://gee.cs.oswego.edu/dl/jmm/cookbook.html<br></div></blockquote>Piggybacking still requires a sync/volatile variable to be touched by both threads in order for the publishing to to be correct (int the AbstractQueueSynchronizer/FutureTaks example, a volatile variable is set to null when reading/writing state, just to enforce happens-before).</div><div>To be more specific, here the safe publishing issue that concerns me:</div><div>T1 creates the Cache on DCM.getCache(). This calls @Inject method on CacheDelegate that would set CacheDelegate.invoker::InterceptorChain. N.B. invoker is not final nor volatile!</div><div>T2 calls CacheDelegate.put, which uses the invoker field set by T1. What guarantee do we have that invoker was correctly published by T1 and seen by T2?&nbsp;</div><div>Looking at the code, the safe publishing takes place in DefaultCacheManager.caches field: this is CHM where T1 added the cache and T2 got it from - so we're safe (published)!</div><div>One more detail, in the same class DefautlCacheManager.globalComponentRegistry doesn't seem to be correctly published (it is not a immutable object as well): this is created by T1 on new DCM and read by T2 when a Cache is instantiated, with no happens-before in between. I think we should declare it volatile. &nbsp; &nbsp;&nbsp;<br><blockquote type="cite"><div><font class="Apple-style-span" color="#540000"><br></font></div></blockquote></div>Cheers,<div>Mircea</div></body></html>