Intrinsic Locks and Monitor Objects
You'll learn: How every Java object carries a built-in lock that synchronized uses to control thread access.
The Built-In Lock Every Object Has
In Java, every single object has an intrinsic lock (also called a monitor lock or simply monitor). This isn't something you create explicitly—it's baked into the language. When you use the synchronized keyword, you're telling Java to acquire this invisible lock.
Think of it like this: every object in Java comes with an invisible key attached to it. When a thread wants to execute a synchronized method or block, it must first grab that object's key. Only one thread can hold the key at a time. When the thread finishes, it releases the key so another thread can take it.
How Synchronized Acquires the Lock
When you write a synchronized method:
public synchronized void increment() {
count++;
}
...the lock being acquired is the intrinsic lock of the object instance (this).
With synchronized blocks, you explicitly specify which object's lock to acquire:
public void increment() {
synchronized(this) { // Acquire THIS object's intrinsic lock
count++;
}
}
The object inside the parentheses is the monitor object—the one whose intrinsic lock controls access.
Lock Acquisition and Release
The lock is automatically acquired when entering the synchronized region and released when exiting (even if an exception occurs). This guarantee prevents deadlocks from forgotten unlocks.
Key Takeaway: Every Java object has an intrinsic lock used by synchronized; acquiring this monitor lock ensures only one thread can execute the protected code at a time.