Explain the synchronized keyword in Java. How does it work?

The synchronized keyword in Java restricts a section of code so that only one thread at a time can execute it, by requiring the thread to first acquire the intrinsic monitor lock associated with a given object or class.

Key Points

Example

Two threads calling a synchronized increment() method on the same Counter object will never execute the method body at the same time — the second thread simply waits until the first one finishes and releases the lock.

public class Counter {
    private int count = 0;

    public synchronized void increment() {
        count++;
    }
}

Interview Tip

A concise interview answer is

synchronized ensures only one thread can execute a given critical section at a time by requiring it to acquire a monitor lock first — on the instance, the class, or an explicit object depending on how it's used. It's reentrant, releases automatically even on exceptions, and guarantees the resulting changes are visible to the next thread that acquires the same lock.

Last updated: 16 July 2026

Previous question: How would you ensure that a shared resource is accessed safely by multiple threads?

Next question: What is the significance of the volatile keyword in Java concurrency?

More Concurrency and Multithreading interview questions

All Concurrency and Multithreading questions