The synchronized keyword in Java controls access to code and data shared between threads. Applied to a method or a block, it makes a thread acquire an object's monitor lock before running, so only one thread at a time executes code guarded by that lock, and changes made under the lock become visible to other threads that later take it.
It solves two problems: race conditions (two threads updating the same data at once) and memory visibility (one thread not seeing another thread's writes). This article covers the modifier forms, which lock each one uses, and when to choose something else.
Every Java object has an intrinsic lock, also called a monitor. synchronized provides:
It does not make an object thread-safe by itself. All access to the shared state, reads included, must use the same lock.
Adding synchronized to an instance method locks the current object (this) for the duration of the call.
The check and the update in withdraw happen atomically, so two threads cannot both pass the balance check and overdraw the account. Two different BankAccount objects have separate locks and do not block each other.
A static synchronized method locks the Class object instead, for example BankAccount.class. This is a different lock from any instance lock, so a static synchronized method and an instance synchronized method can run at the same time.
When only part of a method needs protection, or you want a lock other than this, use a block:
A private lock object prevents outside code from locking this and interfering with your class. See the separate article on the synchronized block for lock selection in detail.
Intrinsic locks are reentrant. A thread that already owns the lock can call another synchronized method on the same object without deadlocking itself.
Some details trip people up:
synchronized cannot be applied to constructors (the object is not shared yet), fields or classes. A constructor can still contain a block.synchronized and must declare it again if needed.synchronized.| Tool | Mutual exclusion | Visibility | Good for |
|---|---|---|---|
synchronized | Yes | Yes | Compound operations on several fields |
volatile | No | Yes | A flag or a single value written by one thread and read by others |
AtomicInteger, LongAdder | Per operation | Yes | Counters and single-variable updates |
ReentrantLock | Yes | Yes | Timeouts, tryLock, interruptible waits, multiple conditions |
| Immutable objects | Not needed | Safe after publication | Shared read-only data |
volatile does not make count++ atomic. For a counter use AtomicInteger.incrementAndGet() or a synchronized method.
Uncontended synchronization is cheap on modern JVMs; the real cost is contention. Keep critical sections short and do not perform blocking I/O while holding a lock.
With virtual threads, blocking inside a synchronized region pinned the carrier thread on Java 21 through 23. Since Java 24 (JEP 491) monitors no longer pin virtual threads in the common case, so the advice to replace every synchronized with ReentrantLock only applies if you still target Java 21.
Guidelines:
this.java.util.concurrent collections and atomics when they fit.An instance method locks this, and a static method locks the Class object. Two threads calling synchronized methods on different instances do not block each other.
Functionally yes for instance methods: synchronized on the method is equivalent to wrapping the body in synchronized (this) { ... }. A block gives you finer scope and a choice of lock.
No. All synchronized instance methods share the object's single monitor, so they exclude each other.
No. It can cause deadlock if threads acquire several locks in different orders.
synchronized gives you mutual exclusion and visibility through an object's monitor. Instance methods lock this, static methods lock the class, and blocks lock the object you name. Use it for compound updates, keep the guarded region small, use private lock objects, and consider atomics, volatile, concurrent collections or ReentrantLock when they match the problem better.