What is the synchronized Keyword in Java?
Learn how Java's synchronized keyword provides mutual exclusion and memory visibility, method vs block locking, static locks, and common interview questions.
Expected Interview Answer
The synchronized keyword in Java is a locking mechanism that ensures only one thread at a time can execute a given synchronized method or block on a particular monitor object, providing both mutual exclusion and memory visibility.
Every Java object has an intrinsic lock (monitor). When a thread enters a synchronized method or block, it acquires that object's monitor and releases it on exit, so other threads must wait. Beyond mutual exclusion, synchronized establishes a happens-before relationship: changes made by one thread before releasing the lock are visible to the next thread that acquires it, preventing stale reads. Synchronized instance methods lock 'this', static synchronized methods lock the Class object, and synchronized blocks lock any explicit object you name.
- Prevents race conditions on shared mutable state
- Guarantees memory visibility via happens-before
- Makes compound read-modify-write operations atomic
- Simple, built into the language with no extra libraries
- Reentrant, so a thread can re-enter locks it already holds
AI Mentor Explanation
A cricket pitch allows only one pair of batters to occupy the crease and score at a time; others wait in the pavilion until it is free. The synchronized keyword is that single-occupancy crease: one thread holds the object's monitor and runs the protected code, while every other thread queues until the lock is released.
Step-by-Step Explanation
Step 1
Identify the shared state
Find the mutable data accessed by multiple threads that needs protection from concurrent modification.
Step 2
Choose the lock object
Decide the monitor: an instance method locks 'this', a static method locks the Class, a block locks a named object.
Step 3
Acquire the monitor
On entering the synchronized region, the thread acquires the object's intrinsic lock, blocking others until it's free.
Step 4
Execute the critical section
Run the read-modify-write logic atomically; no other thread can hold the same lock concurrently.
Step 5
Release and publish
On exit (including via exception), the lock releases and a happens-before edge makes the changes visible to the next acquirer.
Step 6
Keep the region small
Guard only the minimal code that touches shared state to reduce contention and avoid deadlocks.
What Interviewer Expects
- Understanding of mutual exclusion via intrinsic monitors
- Knowledge that synchronized also guarantees visibility (happens-before)
- Difference between synchronized methods and synchronized blocks
- What object is locked for instance vs static synchronized methods
- Awareness of reentrancy and deadlock risks
Common Mistakes
- Thinking synchronized only prevents race conditions but not visibility issues
- Assuming a static and an instance synchronized method share the same lock
- Synchronizing on a mutable or reassigned reference
- Holding the lock over long or blocking operations, hurting throughput
- Believing synchronized methods on different objects block each other
Best Answer (HR Friendly)
“The synchronized keyword lets only one thread at a time run a protected piece of code, like a single key to a room. This stops multiple threads from stepping on each other when they share data, keeping the program's results correct and consistent.”
Code Example
public class Counter {
private int count = 0;
// Locks 'this' for the whole method
public synchronized void incrementMethod() {
count++; // read-modify-write made atomic
}
// Equivalent, but locks only the critical section
private final Object lock = new Object();
public void incrementBlock() {
synchronized (lock) {
count++;
}
}
public synchronized int get() {
return count; // synchronized read sees the latest value
}
}public class IdGenerator {
private static long nextId = 0;
// Locks IdGenerator.class, shared across all instances
public static synchronized long newId() {
return ++nextId;
}
}Follow-up Questions
- What is the difference between a synchronized method and a synchronized block?
- How does synchronized guarantee visibility, not just mutual exclusion?
- What object does a static synchronized method lock?
- How does synchronized compare to ReentrantLock?
- How can two synchronized methods still cause a deadlock?
MCQ Practice
1. What lock does a non-static synchronized instance method acquire?
A synchronized instance method locks the intrinsic monitor of 'this'; different instances therefore use different locks.
2. Besides mutual exclusion, what else does synchronized provide?
Releasing a monitor happens-before another thread acquires it, so writes made under the lock are visible to the next holder.
3. What object does a static synchronized method lock?
A static synchronized method locks the Class object (e.g., IdGenerator.class), which is shared across all instances.
Flash Cards
What does synchronized guarantee? — Mutual exclusion (one thread at a time) plus visibility via a happens-before relationship on lock release/acquire.
Instance vs static synchronized lock? — Instance methods lock 'this'; static methods lock the Class object, so the two do not exclude each other.
Is intrinsic locking reentrant? — Yes. A thread holding a monitor can re-enter other synchronized code on the same object without deadlocking itself.
synchronized method vs block? — A method locks the whole body on 'this' or the Class; a block locks only a chosen region on any named object, reducing contention.