[concurrency-interest] Synchronization question

Ryan LeCompte Ryan.LeCompte at pangonetworks.com
Tue Jun 27 18:22:43 EDT 2006

Phew, now I'm getting confused... :) So what would make my code correct then? Is it sufficient to just mark it volatile in the scenario that I described? 


From: Brian Goetz [mailto:brian at quiotix.com]
Sent: Tue 6/27/2006 6:18 PM
To: Tim Peierls
Cc: Ryan LeCompte; concurrency-interest at cs.oswego.edu; Jeremy Manson
Subject: Re: [concurrency-interest] Synchronization question

And, just for completeness: synchronized write + nonsynchronized read
doesn't create the required happens-before, so it doesn't work.  The
getter in such a case isn't required to ever return anything other than
the default (zero) value.

Tim Peierls wrote:
> Jeremy answered the question, but there's another important point: Code
> that relies on complicated happens-before reasoning is bound to be
> harder to understand and maintain than code with a straightforward
> synchronization policy.
> Volatile access is not expensive. Unless there is a demonstrable
> performance disadvantage in this particular case, you should either make
> the field volatile or synchronize the accessor method. Don't make future
> maintainers of your code read through a justification of why
> non-volatile, non-synchronized access is OK, even if it is. (And
> definitely don't leave out such a justification if you are relying on it!)
> Chapters 2-4 of Java Concurrency in Practice make this point much more
> compellingly than I can here. :-)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: /pipermail/attachments/20060627/2374d3bd/attachment.html 

More information about the Concurrency-interest mailing list