[concurrency-interest] Class variables and concurrency
dholmes at dltech.com.au
Sun Nov 6 21:35:00 EST 2005
Dawid Kurzyniec writes:
> Is the same guaranteed if the field is non-final?
Yes. The initialization of a static field (via an initialization expression
or static initializer block) is guaranteed to be seen. Of course if the
field is non-final and changed after initialization that is a different
> Does this mean that every access to any class variable involves locking?
> Or is there some kind of a thread-local cache so that each thread locks
> on any class at most once? What does the specification say about it?
The specification requires that before any action that requires a class to
be initialized is performed, the class must be determined to be initialized
by acquiring the class lock as I described. See JVMS Section 2.17.5.
Logically this means that every thread must appear to acquire the Class
object lock of every Class/Interface it uses (where such use requires
initialization) at least once. The VM will do the best it can to try and
ensure it is at most once. For example, the JIT can see if a class to be
used by a jitted method has been initialized or not, and if so generate code
that skips the initialization check; otherwise it puts in the initialization
check on the first use. There are various ways the VM can optimise this.
More information about the Concurrency-interest