[concurrency-interest] Assignment of references atomic?

Tim Peierls tim at peierls.net
Wed Oct 12 09:58:10 EDT 2005

Ryan LeCompte wrote:
> Thank you for the lengthy response. This particular part of the code can tolerate stale values
> that are returned from the getObject() method. However, I want to be able to rely on the fact
> that the getObject() method will EVENTUALLY return a recent update to the this.object (o)
> field. Am I safe to make that assumption? Or is it possible for the JVM to always return a
> stale value even if the value gets updated one more time?

That assumption is not safe. No matter how many times a thread updates the variable, other threads 
will see arbitrarily stale values.

> What about declaring the variable volatile?

Ah! Yes, volatile *does* work, and is the most appropriate mechanism. I should have mentioned that 
first, sorry.

The only reason you might *not* want to use a volatile is if there is a class invariant involving 
more than one field, including the object field. I saw the use of a variable named someCondition 
and assumed this was the case.


More information about the Concurrency-interest mailing list