The most synchronization idiom in Java is using the synchronized
keyword.
The JVM supports synchronization of both methods and sequences of instructions within a method using a single synchronization construct - synchronized
- which is built around an internal entity known as the intrinsic lock
or monitor lock
(The API specification often refers to this entity simply as a "monitor"). Intrinsic locks play a role in both aspects of synchronization: enforcing exclusive access to an object's state and establishing happens-before
relationships to ensure a correct visibility amoung threads.
At bytecode level, the JVM supplies the monitorenter
and monitorexit
instructions to support such constructs.
At runtime level, in the Java HotSpot VM, every object has a built-in monitor (the so called intrinsic lock
or monitor lock
) that can be acquired by any thread.