[concurrency-interest] a question about concurrent safe access

Doug Lea dl at cs.oswego.edu
Mon Sep 19 07:19:20 EDT 2005

Yechiel Feffer wrote:
> Hi all
> say I have a pointer ( reference) to an object ( A). This reference is 
> changed to point at object B. I have threads that concurrently are using 
> that reference to get the object it points at. I dont want to 
> synchronize the usage of the reference. Is it safe to do so, i.e. if I 
> dont use a lock and I dont declare the reference volatile will I always 
> get object A or B or do I have a risk of getting "dirty" inconsistent 
> pointer , i.e. is assigning a pointer always atomic  ?

Pleae declare the field volaitle.

One of the goals of the JSR133 JMM revision was to make answers to
questions like this nearly automatic: If you have a field that is not
consistently protected by some kind of synchronization, then it should
either be declared volatile or final. (Final doesn't apply here.)

If you don't declare it volatile, then you have to be prepared for
suprising things to happen, like begin able to read the reference
but not seeing the current values of the fields of the referred-to
object. The gruesome details are in chapter 17 of the revised JLS


More information about the Concurrency-interest mailing list