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

Kirk Pepperdine kirk at kodewerk.com
Fri Jul 7 05:41:41 EDT 2017


> On Jul 6, 2017, at 11:29 PM, Stephen Colebourne <scolebourne at joda.org> 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).

I’m sorry but of the reasons I previously stated, this is broken.

— Kirk



More information about the Concurrency-interest mailing list