VOOZH about

URL: https://www.geeksforgeeks.org/java/difference-between-lock-and-monitor-in-java-concurrency/

⇱ Difference Between Lock and Monitor in Java Concurrency - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Difference Between Lock and Monitor in Java Concurrency

Last Updated : 7 Apr, 2026

Lock and Monitor in Java are both used to manage synchronization and control access to shared resources in multithreading. They help ensure thread safety but differ in flexibility and usage.

  • Both are used to achieve synchronization in concurrent programming
  • Monitor is implicit (synchronized), while Lock is explicit and more flexible
  • Lock provides advanced features compared to basic Monitor functionality

Monitor

A monitor in Java is a mechanism used to control access to shared resources in multithreading. It ensures that only one thread can access a synchronized block or method at a time.

  • Every Java object has an intrinsic lock and can act as a monitor.
  • Only one thread can hold the monitor lock at a time; others must wait.
  • Used with synchronized, wait(), notify(), and notifyAll() for thread communication.

Output
Geeks for Geeks

Explanation: Only one thread prints at a time because the synchronized method locks the monitor.

Lock in Java

Lock is a tool for controlling access to shared resources by multiple threads. It’s part of the java.util.concurrent.locks package, introduced in Java 5 and is an alternative to the traditional synchronized keyword.

Below is the illustration which demonstrates the functioning of basic locks.

πŸ‘ Locks
Locks

Output
Final value of sharedResource: 2000

Explanation:

  • sharedResource is a common variable accessed by two threads.
  • ReentrantLock ensures only one thread updates it at a time.
  • Both threads increment the value 1000 times each.
  • lock.lock() and lock.unlock() provide safe access.
  • Final output = 2000, proving thread-safety.

Lock vs Monitor

AspectMonitor (synchronized)Lock (ReentrantLock)
OriginJVM intrinsic, low-level primitiveJava 5, high-level API
ImplementationImplicit, JVM-managedExplicit, programmer-managed
Critical Section ManagementAutomaticManual (lock() / unlock())
Thread QueueingJVM manages waiting threadsProgrammer can choose fairness policies
FeaturesSimple mutual exclusionAdvanced: fairness, interruptible, tryLock, multiple conditions
PerformanceLightweight for small threadsSlightly higher overhead, but more flexible
UsageSimple synchronization, small thread poolsComplex synchronization, high concurrency scenarios
Deadlock HandlingLess controlMore explicit control but can still occur

Note: As monitors themselves are implemented with the necessary support of locks, it is often said that they are not different but complementary in their existence.

Comment
Article Tags: