Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

What is the purpose of the synchronized keyword in Java?

Java
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedNov 12, 2024
UpdatedNov 16, 2026
5 min read

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.

Table of Contents

  • What synchronized guarantees
  • Synchronized instance methods
  • Static synchronized methods
  • Synchronized blocks
  • Reentrancy and what is not allowed
  • synchronized compared with volatile, atomics and locks
  • Performance, virtual threads and good practice
  • Frequently Asked Questions
    • What does a synchronized method lock?
    • Is a synchronized method the same as a synchronized block on this?
    • Can two threads run different synchronized methods of one object at once?
    • Does synchronized prevent deadlock?
  • Summary

What synchronized guarantees

Every Java object has an intrinsic lock, also called a monitor. synchronized provides:

  • Mutual exclusion: at most one thread holds a given monitor at any time.
  • Visibility: when a thread releases a monitor, its writes happen-before the actions of the next thread that acquires the same monitor.
  • Atomicity of the guarded section relative to other code guarded by the same lock.

It does not make an object thread-safe by itself. All access to the shared state, reads included, must use the same lock.

Synchronized instance methods

Adding synchronized to an instance method locks the current object (this) for the duration of the call.

java

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.

Static synchronized methods

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.

java

Synchronized blocks

When only part of a method needs protection, or you want a lock other than this, use a block:

java

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.

Reentrancy and what is not allowed

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.
  • The modifier is not part of the method signature. An overriding method does not inherit synchronized and must declare it again if needed.
  • In an interface, abstract methods cannot be synchronized.
  • An uncaught exception releases the lock.

synchronized compared with volatile, atomics and locks

ToolMutual exclusionVisibilityGood for
synchronizedYesYesCompound operations on several fields
volatileNoYesA flag or a single value written by one thread and read by others
AtomicInteger, LongAdderPer operationYesCounters and single-variable updates
ReentrantLockYesYesTimeouts, tryLock, interruptible waits, multiple conditions
Immutable objectsNot neededSafe after publicationShared read-only data

volatile does not make count++ atomic. For a counter use AtomicInteger.incrementAndGet() or a synchronized method.

Performance, virtual threads and good practice

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:

  1. Guard all access to a piece of shared state with the same lock.
  2. Prefer private final lock objects over this.
  3. Take multiple locks in a fixed order to avoid deadlock.
  4. Prefer java.util.concurrent collections and atomics when they fit.
  5. Prefer immutability so you need fewer locks.

Frequently Asked Questions

What does a synchronized method lock?

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.

Is a synchronized method the same as a synchronized block on this?

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.

Can two threads run different synchronized methods of one object at once?

No. All synchronized instance methods share the object's single monitor, so they exclude each other.

Does synchronized prevent deadlock?

No. It can cause deadlock if threads acquire several locks in different orders.

Summary

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.

Share