Skip to main content

Java Concurrency - Intrinsic Lock & Reentrancy

Greetings!

Any Java object can implicitly act as a lock for purposes of synchronization. These built-in locks are intrinsic locks.

Java provides synchronized block as a built-in locking mechanism to enforce atomicity.

Java built in object lock is called Intrinsic Lock.


synchronized (lock) {
    // shared data
}


  • When use in method level, invoking object is the lock.
  • Act as a mutex (only one thread at a time)
  • Lock is acquired by the executing thread before entering a synchronized block.

Reentrancy

Thread owning a lock can again acquire the lock is called reentrant lock. Can claim the lock multiple time without blocking itself.
  • Without reentrant lock, there will be a dead lock.
  • Intrinsic locks are re-entrant.
  • Locks are acquired on a per-thread basis rather than per-invocation basis.

Every shared, mutable state should be guarded by the same lock.



Comments