A synchronized method locks an entire method body using an implicit lock — the instance for a non-static method or the Class object for a static one — while a synchronized block lets you lock only a specific, smaller section of code on a lock object you choose explicitly.
Key Points: • A synchronized instance method locks on "this", so no thread can enter any other synchronized instance method on that same object while one is executing. • A synchronized static method locks on the Class object itself, which is a separate lock from any instance-level locking. • A synchronized block can target a dedicated lock object distinct from "this", allowing finer-grained control over exactly what's protected and by which lock. • Reducing the synchronized region to only the truly critical code (rather than the whole method) reduces contention and improves throughput. • Overuse of synchronized methods on a busy object can cause unrelated operations to block each other unnecessarily if they don't actually touch the same shared state.
Example: If only a small part of a method actually touches shared state, wrapping just that portion in synchronized(lockObject) { ... } instead of marking the whole method synchronized lets unrelated, non-critical parts of the method run without contending for the lock.
Code Example:
// Synchronized method - locks the whole method on 'this'
public synchronized void updateAll() {
// entire method is the critical section
}
// Synchronized block - locks only the critical section
public void updatePartial() {
// non-critical setup work here
synchronized (this) {
// only this part is the critical section
}
}Interview Tip: A concise interview answer is:
"A synchronized method locks the whole method body on an implicit lock — the instance or the class for static methods — while a synchronized block lets you scope the lock to just the critical lines and choose the lock object yourself. Blocks are generally preferred when only part of a method needs protection, since they reduce how long the lock is held."