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

Jeremy Manson jmanson at cs.umd.edu
Wed Dec 20 13:16:51 EST 2006


Kessel, Chris wrote:
> The "private" has nothing to do with it. It's just a common default
> declaration for instance fields.  I didn't mean to imply it had anything
> to do with threading.
> 
> The question was in the first part really.  Must a thread safe class
> have every instance field protected by volatile, final, or synchronized
> blocks?

Oh, I see.  Well, the answer is "more or less" (and assuming you mean to 
include j.u.c locks under the aegis of "synchronized blocks").  There 
are other corner cases where some protection is offered.  For example, 
if one thread creates an object, places a reference to that object in a 
global variable, and then spawns another thread, then that other thread 
can read the object without using final, volatile or synchronized() 
(assuming no other thread is involved, and nobody writes to the object 
after the thread is spawned).  Similarly with a join.

					Jeremy


More information about the Concurrency-interest mailing list