Synchronized Method vs Synchronized Block
Fast recall
Quick revision
Core answer
synchronized method locks this; a static synchronized method locks the declaring Class object. A synchronized block locks the chosen expression for only the enclosed code. A smaller block may reduce contention, but it is not automatically faster: every access that belongs to the invariant must still use the same lock, and blocking I/O should normally not occur while holding it.Understanding of synchronization granularity and the ability to choose appropriate locking strategies.
Not knowing you can specify a different lock object in a synchronized block, or thinking synchronized methods and blocks are completely interchangeable.
Explain that synchronized blocks reduce the critical section size, improving concurrency by allowing multiple threads to execute non-conflicting parts of the method simultaneously.
Interview-ready guide
Interview answer
A synchronized instance method locks the receiver, while a static synchronized method locks that class's Class object. A synchronized block lets code name another shared monitor and limit the protected region to the operation that must be atomic.
For example, synchronized void deposit(...) locks the whole instance method. If the method first performs independent validation and only the balance update needs mutual exclusion, synchronized (balanceLock) { balance += amount; } can keep the unrelated work outside the critical section. The block is correct only if every access participating in the balance invariant uses that same balanceLock. I choose the form by ownership and clarity, not by assuming a block is always faster, and I avoid holding either form of monitor while doing slow I/O.
Synchronized Method vs Synchronized Block
Direct Answer
An instance synchronized method locks this; a static synchronized method locks the declaring Class object. A synchronized block locks the chosen expression for only the enclosed code. A smaller block may reduce contention, but it is not automatically faster: every access that belongs to the invariant must still use the same lock, and blocking I/O should normally not occur while holding it.
Code Example
public class Counter {
private int count;
private final Object lock = new Object(); // dedicated private monitor
// Synchronized method: locks the entire 'this' object for the whole method
public synchronized void incMethod() {
count++;
}
// Synchronized block: only the critical line is serialized
public void incBlock() {
prepare(); // runs concurrently — not under the lock
synchronized (lock) {
count++; // narrow critical section = less contention
}
}
private void prepare() { /* expensive, thread-safe prep */ }
}
Locking on a private final object (not on this) stops external code from accidentally locking the same monitor.
Common Mistakes & Pitfalls
- Synchronizing on a local variable: Freshers write synchronized on a variable declared inside the method, so each thread gets its own lock instance and no coordination happens—the lock object must be shared across threads.
- Mixing method and block locks on the same object: If some methods use synchronized method (locking on this) and others use synchronized block on a different object, they don't coordinate, and you still have race conditions.
- Locking on a collection you're iterating: Synchronizing on a list object while iterating doesn't prevent concurrent modification if other code adds/removes elements without locking the same object—use explicit lock objects or concurrent collections.
Check your understanding
Answer these without looking back at the article.
- Can you use a different object as the lock in a synchronized block?
- What is lock contention?
- When would you use a private Object as a lock instead of 'this'?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.