A monitor in Java is a synchronization mechanism that controls concurrent access to an object. It ensures that only one thread can execute critical section (a block of code that accesses shared resources) at a time, while other threads wait. When a thread enters a synchronized block:
- It requests the monitor of the object.
- If the monitor is available, it acquires it and executes the synchronized code.
- If another thread already holds the monitor, the new thread waits until the monitor is released.
- After execution, the thread releases the monitor, allowing other threads to acquire it
Key Concepts of Monitors
1. Mutual Exclusion
Mutual Exclusion ensures that only one thread can execute a synchronized section of code at a time. Example: Only one thread can update a shared variable at a time.
2. Thread Coordination
Thread Coordination allows threads to communicate and coordinate with each other using:
- wait() -> Thread releases the monitor and waits.
- notify() -> Wakes up one waiting thread.
- notifyAll() -> Wakes up all waiting threads.
Example: Implementing a Monitor Using the synchronized Keyword
class SharedResource{
void printTable(int n) {
// Monitor lock on current object
synchronized(this) {
for (int i = 1; i <= 2; i++) {
System.out.println(n * i);
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println(e.getMessage());
}
}
}
}
}
class MyThread1 extends Thread {
SharedResource resource;
MyThread1(SharedResource resource) {
this.resource = resource;
}
public void run() {
resource.printTable(5);
}
}
class MyThread2 extends Thread {
SharedResource resource;
MyThread2(SharedResource resource) {
this.resource = resource;
}
public void run() {
resource.printTable(10);
}
}
public class GFG{
public static void main(String[] args) {
SharedResource obj = new SharedResource();
MyThread1 t1 = new MyThread1(obj);
MyThread2 t2 = new MyThread2(obj);
t1.start();
t2.start();
}
}
Output:

Explanation
- The synchronized (this) block acquires the monitor lock of the current object.
- When one thread executes the printTable() method, the other thread must wait until the first one releases the monitor.
- This ensures mutual exclusion, preventing data inconsistency and race conditions.
Note: The output order may vary depending on thread scheduling, but due to synchronization, one thread completes its entire loop before the other begins.
Monitor vs Lock
| Feature | Monitor | Lock |
|---|---|---|
| How it works | Built-in synchronization mechanism using the synchronized keyword. | A manual locking mechanism from java.util.concurrent.locks package. |
| Control | Automatically handled by JVM, no need to explicitly lock or unlock. | Developer manually controls access using lock() and unlock(). |
| Flexibility | Simple but limited, cannot try or interrupt while waiting. | Very flexible , supports tryLock(), lockInterruptibly(), and fairness options. |
| Thread Communication | Uses wait(), notify(), and notifyAll() methods. | Uses Condition objects for better thread coordination. |
| Performance | Easier to use but slower under heavy thread contention. | More efficient and scalable for high-concurrency applications. |