BLOCKED State: Waiting for Monitor Lock
You'll learn how threads enter the BLOCKED state when competing for synchronized resources.
The Core Idea
When a thread attempts to enter a synchronized block or method but another thread already holds that lock (called a monitor lock), the waiting thread enters the BLOCKED state. It's stuck at the door, unable to proceed until the lock becomes available.
Think of it like a single-occupancy restroom with a lock. If someone's inside (holding the monitor), you stand outside the door (BLOCKED state) waiting for them to unlock it. You're not doing anything productive—you're just waiting for access.
How It Works
- Thread A acquires a monitor lock on an object by entering a
synchronizedblock - Thread B tries to enter a
synchronizedblock on the same object - Thread B immediately transitions from RUNNABLE to BLOCKED
- Thread B remains BLOCKED until Thread A releases the lock (exits the synchronized block)
- Once the lock is free, Thread B competes to acquire it and returns to RUNNABLE
The Mechanism
Object lock = new Object();
// Thread A
synchronized(lock) {
// Thread A holds the monitor lock
// Any work here...
}
// Thread B tries to enter while A is inside
synchronized(lock) { // Thread B becomes BLOCKED here
// Can only enter after A releases the lock
}
Thread B cannot execute any code inside the synchronized block until it acquires the lock. The JVM scheduler doesn't even consider it for CPU time during this period—it's genuinely blocked.
Key Takeaway: The BLOCKED state occurs exclusively when threads compete for synchronized locks; a thread enters BLOCKED automatically when it tries to acquire a monitor lock that's currently held by another thread, and exits BLOCKED only when it successfully acquires that lock.