<div dir="ltr"><div>Due to issues for years trying to understand the lifecycle of class/value pairs stored in ClassValue, I am only now integrating the JDK-provided version of ClassValue into JRuby's logic to store method tables for Java classes. And I have run into a new inconsistency I want to clarify.</div><div><br></div><div>The docs for ClassValue state this for the "computeValue" method:</div><div><br></div><div>"This method will be invoked within the first thread that accesses the value with the get method.</div>Normally, this method is invoked at most once per class, but it may be invoked again if there has been a call to remove."<div><br></div><div>To me, that means computeValue will be invoked *exactly once* per class (ignoring removals), regardless of how many threads attempt to compute it at the same time. Otherwise, what's the point of saying it will be invoked by the "first thread"?</div><div><br></div><div>But then the docs for "get" say something different:</div><div><br></div><div>"Returns the value for the given class. If no value has yet been computed, it is obtained by an invocation of the computeValue method.<br>The actual installation of the value on the class is performed atomically. At that point, if several racing threads have computed values, one is chosen, and returned to all the racing threads."</div><div><br></div><div>Ok hold up... so now it's possible for multiple threads to independently computeValue? These two statements don't appear to mesh... I'm looking for the behavior computeValue describes: basically computeIfAbsent. But in practice (and from what I have read of the current implementation), multiple threads might call computeValue for a given class.</div><div><br></div><div>In JRuby, where the computation of this value also sets up global namespace tables, it results in warnings that the namespace entry has been initialized multiple times.</div><div><br></div><div>Adding synchronized to my implementation of computeValue does not help; it just means two threads don't compute at the same time. They will block until the first thread finishes its computeValue, and while that first thread is initializing the ClassValue, they'll proceed to computeValue several more times.</div><div><br></div><div>This was not a problem with my home-grown ClassValue implementation because I double-check the cache before proceeding into synchronized code that makes the computeValue call (computeIfAbsent behavior).</div><div><br></div><div>I can work around this by also overriding ClassValue.get to be synchronized, but the existing behavior does not seem right to me. It works this way on 1.8, 21, and 24, so nothing has changed. Either the docs are wrong or the implementation is wrong.</div><div><br><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><b>Charles Oliver Nutter</b><div><i>Architect and Technologist</i></div><div>Headius Enterprises</div><a href="https://www.headius.com" target="_blank">https://www.headius.com</a><div><div><a href="mailto:headius@headius.com" target="_blank">headius@headius.com</a></div></div></div></div></div></div></div>