synchronized Keyword in Java
Fast recall
Quick revision
Core answer
synchronized method or block acquires an object's monitor. Only one thread at a time can hold that same monitor, so code using the same lock can protect a critical section. Releasing the monitor happens-before a later acquisition of it, which also provides visibility. It does not protect code that uses a different lock or no lock.Core multithreading knowledge—synchronized is the most fundamental synchronization mechanism in Java.
Not knowing what object the lock is acquired on, or thinking synchronized only prevents simultaneous execution without understanding the memory visibility guarantee.
Explain the intrinsic lock concept and the difference between synchronized method versus synchronized block, including which object holds the lock in each case.
Interview-ready guide
Interview answer
synchronized protects a critical section by acquiring an object's monitor. If Thread A owns that monitor, Thread B cannot enter another section guarded by the same monitor until A releases it. An instance synchronized method uses this; a static synchronized method uses the class object; a block lets me name another lock.
It also creates a Java Memory Model edge: writes before releasing a monitor are visible after another thread later acquires that same monitor. The phrase 'same monitor' is essential. Synchronizing a writer on lockA and a reader on lockB gives no shared ordering. I choose the lock around the state invariant, and I keep the block focused without splitting an operation that must remain atomic.
synchronized Keyword in Java
Direct Answer
A synchronized method or block acquires an object's monitor. Only one thread at a time can hold that same monitor, so code using the same lock can protect a critical section. Releasing the monitor happens-before a later acquisition of it, which also provides visibility. It does not protect code that uses a different lock or no lock.
Common Mistakes & Pitfalls
- Synchronizing on the wrong object: Freshers often synchronize on different objects thinking they're protecting the same data—if Thread A locks on
lockAand Thread B locks onlockB, both enter the critical section simultaneously. The lock object must be shared. - Synchronizing local variables: Writing
synchronized(localVar)in a method where each thread gets its ownlocalVarinstance provides zero protection—you need a shared lock object. - Forgetting static synchronized locks the Class object: A static synchronized method doesn't lock instance methods, so instance and static synchronized methods can run concurrently, potentially causing issues if they touch shared static fields.
Check your understanding
Answer these without looking back at the article.
- What is an intrinsic lock in Java?
- What is the difference between synchronized method and synchronized block?
- Does synchronization affect performance?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.