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

Doug Lea dl at cs.oswego.edu
Fri Jul 7 09:01:10 EDT 2017


On 07/07/2017 08:27 AM, Alex Otenko wrote:
> If you are updating a reference, then CAS can also work. If you are
> talking about imitating update of the reference by mutating inlined
> object contents, then you do need synchronized for readers.

Or use a StampedLock, with initial optimistic read, falling
back to read-lock, as in the javadoc examples for StampedLock:
http://gee.cs.oswego.edu/dl/jsr166/dist/docs/java/util/concurrent/locks/StampedLock.html

(This is an extension of the SC-write, acquire-read pattern in
my last post.)

-Doug



> 
> Alex
> 
>> On 7 Jul 2017, at 10:32, Andrew Haley <aph at redhat.com> wrote:
>> 
>> On 07/07/17 10:23, Alex Otenko wrote:
>>> No, not only updates, but reads, too.
>>> 
>>> If you assume non-atomic updates, the problem becomes just like
>>> with volatile long - has to use synchronized block for all
>>> accesses.
>> 
>> No.  There is one reference to the Instant, reference updates are 
>> atomic, and Instant itself is immutable, so none of its state
>> becomes visible before all of its fields are initialized.  I do not
>> believe that it is necessary for readers to be synchronized.
>> 
>> -- Andrew Haley Java Platform Lead Engineer Red Hat UK Ltd.
>> <https://www.redhat.com> EAC8 43EB D3EF DB98 CC77 2FAD A5CD 6035
>> 332F A671
> 
> _______________________________________________ Concurrency-interest
> mailing list Concurrency-interest at cs.oswego.edu 
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
> 



More information about the Concurrency-interest mailing list