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

Aleksey Shipilev shade at redhat.com
Mon Jul 10 06:06:22 EDT 2017

On 07/10/2017 12:02 PM, Alex Otenko wrote:
> Thanks, this is very useful.
> What about
> Runnable r1 = () -> {};
> Runnable r2 = r1;
> assert r1 == r2;
> should we expect a bombshell here? Clearly, autoboxing/unboxing is allowed to happen, if, say, r1 and r2 were Integers, but which side of the surprising behaviour will the JVM choose?

I think the answer is the same as with Integers, if you rephrase your example
with them:

  Integer i1 = 4242;
  Integer i2 = i1;   // this is not autoboxing anymore, this is reference store
  assert (i1 == i2); // would you expect this to fail?

Similarly, the key part is here:
  Runnable r2 = r1;  // this is not a lambda-expression, this is reference store


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: OpenPGP digital signature
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20170710/c944f342/attachment-0001.sig>

More information about the Concurrency-interest mailing list