[concurrency-interest] Class variables and concurrency

David Holmes dholmes at dltech.com.au
Sun Nov 6 20:23:32 EST 2005


> Dawid Kurzyniec writes:
> Now, I was responding to your fix: for it to be 100% correct, you need
> that "final". To my understanding (experts correct me if I'm wrong), you
> may otherwise see this field uninitialized (i.e. null) in some threads.

You will never see a static final field with its default initialized value.
The synchronization that occurs as part of the class initialization process
make this impossible. The lock of the Class object is held during static
initialization of the class, and any use of the class that requires that it
be initialized also must acquire that lock. So no thread can use the class
without having gained the lock, which is only released after initialization
is complete. The release of the lock by the initializing thread (and thus
all initialization code) happens-before the lock acquisition by another
thread.

The reason you need the final here is if the field remains non-private,
otherwise some other thread could change the value.

Cheers,
David Holmes



More information about the Concurrency-interest mailing list