[concurrency-interest] Should I avoid compareAndSet with value-based classes?

Brian S O'Neill bronee at gmail.com
Thu Jul 6 17:52:05 EDT 2017


It sounds like the documentation should be updated to prevent further 
confusion. Perhaps this example (or something like it) should be provided.

On 2017-07-06 02:29 PM, Stephen Colebourne wrote:
> Given:
> 
> Instant i1 = Instant.ofEpochMilli(2);
> Instant i2 = Instant.ofEpochMilli(2);
> 
> In Java 8:
> 
> i1.equals(i2) is true
> i1 == i2  is false
> 
> And in some future version, i1 == i2  might be true.
> 
> But this is not especially radical. Java 10 could introduce a cache in
> the factory method that always returns the same instance. In fact,
> there already is a cache if you call
> Instant.ofEpochMilli(0);
> 
> The key phrase in
> http://download.java.net/java/jdk9/docs/api/java/lang/doc-files/ValueBased.html
> is "A program may produce unpredictable results if it attempts to
> distinguish two references to equal values of a value-based class".
> IMO, the intent is to tell everyone that they shouldn't try to tell
> the difference between two instances using ==, and if they do their
> program may change meaning in the future (ie. be unpredictable, but
> not stupidly unpredictable as some in the thread are suggesting).
> 
> Stephen
> 
> 


More information about the Concurrency-interest mailing list