[concurrency-interest] Synchronization question

Brian Goetz brian at quiotix.com
Tue Jun 27 23:29:25 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?

Quoting from JCiP 3.5.3 (Safe Publication Idioms), p52:

To publish an object safely, both the reference to the object and the 
object's state must be made visible to other threads at the same time. 
A properly constructed object can be safely published by:

  - Initializing an object reference from a static initializer;
  - Storing a reference to it into a volatile field or AtomicReference;
  - Storing a reference to it into a final field of a properly 
constructed object;
  - Storing a reference to it into a field that is properly guarded by a 
lock.

The last one, properly guarded by a lock, means all accesses (read and 
write) to the field synchronize on a common lock.

If you want more, buy the book :)



More information about the Concurrency-interest mailing list