[concurrency-interest] Basic thread safety question: final/volatile/synchronized fields

Brian Goetz brian at quiotix.com
Wed Dec 20 16:19:12 EST 2006

> Having read JCiP, I find I'm quite paranoid now about thread safety :).

Then you read it right!

> Is it correct to say that a thread safe class requires (though this is
> not necessarily sufficient) that every instance field be final,
> volatile, or accessed in synchronized blocks?

There are edge cases, but to first order, yes.  (To be clear, insert 
"exclusively" after "accessed" in the above sentence.

> The default in a lot of code, and my default for years, has been to
> declare object fields "private Foo _f;" in objects that end up being
> used in multi-threaded contexts.  Based on my understanding now, that
> can't be a thread-safe field (unless used in synchronized blocks).

It is possible that the locking is higher up on the call stack.  For 

class Foo { public int x; }


class Moo {
   @GuardedBy("this") private Foo foo = new Foo();

   public void doSomething() {
       synchronized (this) {

The field x is accessed in a thread-safe manner in Moo, but the class 
Foo is not thread-safe.  The thread-safety is provided by the locking in 

More information about the Concurrency-interest mailing list