Multithreading
Synchronized Methods
Understand instance-level synchronized methods and how they lock on the object instance.
Interview: Commonly tests how synchronized instance methods coordinate locks, and what happens when multiple threads call different synchronized methods on the same object.
In Java, tagging an instance method as synchronized forces a thread to acquire the monitor lock of the object instance before executing the method body.
Core Idea
A synchronized instance method implicitly locks on "this". Only one thread can enter any synchronized instance method of that object.
Why It Matters
It provides a simple way to implement thread-safety for instance variables within a class.
Interview Lens
Tests if a thread running a synchronized method blocks other threads from running non-synchronized methods on the same object.
Locking Mechanics
When a thread invokes a synchronized instance method:
- It automatically acquires the monitor lock of the calling object (
this). - If another thread already holds that lock, the invoking thread is suspended and enters the BLOCKED state.
- Non-synchronized methods do NOT require a lock and can be executed concurrently by other threads without blocking.
Code Walkthrough
The following example demonstrates that while a thread holds the lock, other threads are blocked from entering other synchronized methods on the same instance.
public class SynchronizedMethodsDemo { public synchronized void methodA() { System.out.println(Thread.currentThread().getName() + " inside methodA"); try { Thread.sleep(2000); // Hold lock for 2 seconds } catch (InterruptedException e) { Thread.currentThread().interrupt(); } System.out.println(Thread.currentThread().getName() + " exiting methodA"); }public synchronized void methodB() { System.out.println(Thread.currentThread().getName() + " inside methodB"); }
public static void main(String[] args) { SynchronizedMethodsDemo demo = new SynchronizedMethodsDemo();
// Thread 1 enters methodA and acquires lock new Thread(demo::methodA, "Thread-1").start();
// Thread 2 tries to enter methodB on the same instance, but blocks new Thread(demo::methodB, "Thread-2").start(); } }
Interview-Relevant Information
Q: If Thread 1 is executing a synchronized instance method on an object, can Thread 2 execute a non-synchronized method on the same object?
Answer: Yes, absolutely. Non-synchronized methods do not attempt to acquire the monitor lock of the object and can run concurrently without checking lock status.
Quick Checklist
What object does a synchronized instance method lock on? Can other threads call non-synchronized methods? If yes, you understand instance synchronization.
Use Cases
Safeguarding state transitions in simple domain model objects.
Implementing thread-safe wrapper classes.
Common Mistakes
Thinking synchronized methods lock on the class (they lock on individual instances; different instances have independent locks).
Making getter methods non-synchronized when setters are synchronized, creating dirty reads/visibility bugs.