Java Concurrency - Monitor (Intrinsic|Implicit) Lock

Java Conceptuel Diagram

About

Synchronization is built around an internal object property known as:

  • intrinsic lock
  • monitor lock
  • implicit lock
  • or simply monitor.

Each object in Java is associated with a monitor, which a thread can lock or unlock.

  • Only one thread at a time may hold a lock on a monitor.
  • Any other threads attempting to lock that monitor are blocked until they can obtain a lock on that monitor.

See Concurrency - Lock (Mutex)

Intrinsic locks play a role in both aspects of synchronization:

  • enforcing exclusive access to an object's state
  • and establishing happens-before relationships that are essential to visibility.

By convention, a thread that needs exclusive and consistent access to an object's fields has to acquire the object's intrinsic lock before accessing them, and then release the intrinsic lock when it's done with them.

All lock acquisition and release to occur in a block-structured way: when multiple locks are acquired they must be released:

  • in the opposite order,
  • in the same lexical scope in which they were acquired.

A thread is said to own the intrinsic lock between the time it has acquired the lock and released the lock.

As long as a thread owns an intrinsic lock, no other thread can acquire the same lock.

The other thread will block when it attempts to acquire the lock.

When a thread releases an intrinsic lock, a happens-before relationship is established between that action and any subsequent acquisition of the same lock.

A thread cannot acquire a lock owned by another thread but a thread can acquire a lock that it already owns. Allowing a thread to acquire the same lock more than once enables reentrant synchronization.

This kind of lock is easy to use, but has many limitations. More sophisticated locking idioms are supported by the java.util.concurrent.locks package. See Java Concurrency - Lock Objects (java.util.concurrent.locks)

Level

Class

Class level lock it the synchronized method is static

Object

Object level lock it the synchronized method is instantiated (ie not static)

Documentation / Reference





Discover More
Data System Architecture
Concurrency - Guarded block (Thread Communication Idiom)

The most common coordination idiom between thread is the guarded block. Such a block begins by polling a condition that must be true before the block can proceed. The loop is wasteful, since it...
Java Conceptuel Diagram
Java - Concurrency (Parallel Work)

In concurrent or parallel programming, there are two basic units of execution: processes. A process is implemented as a main thread that can create other thread. and threads. Threads exist within...
Java Conceptuel Diagram
Java Concurrency - (Object) Wait

in Java. java/lang/ObjectObject.wait, java/lang/Object.htmlwait(long timeout) and java/lang/Objectwait(long timeout, int nanos) suspend the current thread. The invocation of wait does not return until...
Java Conceptuel Diagram
Java Concurrency - (Queue|Stack)

and in Java concurrency context Interface and Implementation The java/util/concurrent/BlockingQueueBlockingQueue interface defines a first-in-first-out data structure that blocks or times out...
Java Conceptuel Diagram
Java Concurrency - Fine-grained synchronization

Synchronized statements are also useful for improving concurrency with fine-grained synchronization. Suppose: class MsLunch has two instance fields, c1 and c2, c1 and c2 are never used together....
Java Conceptuel Diagram
Java Concurrency - Happens-before Relationship

in Java The following actions create happens-before relationships: synchronized construct. When a thread releases an intrinsic lock, a happens-before relationship is established between that action...
Java Conceptuel Diagram
Java Concurrency - Lock Objects (java.util.concurrent.locks)

java/util/concurrent/locks/package-summaryLock Object package is a High Level Concurrency of the java.util.concurrent. For low-level, intrinsic lock (monitor), see . There is interfaces and classes that...
Java Conceptuel Diagram
Java Concurrency - Reentrant Lock

A thread cannot acquire a lock owned by another thread but a thread can acquire a lock that it already owns. Allowing a thread to acquire the same lock more than once enables reentrant synchronization....
Java Conceptuel Diagram
Java Concurrency - Synchronization (Thread Safety)

in java. Java offers two basic synchronization idioms: synchronized methods synchronized statements final fields, which cannot be modified after the object is constructed, can be safely read...
Java Conceptuel Diagram
Java Concurrency - Synchronized Statement

Another way to create synchronized code is with synchronized statements. Unlike synchronized methods, synchronized statements must specify the object that provides the intrinsic lock. The synchronized...



Share this page:
Follow us:
Task Runner