Java.Multithreading.Give me details about monitor, i cannot understand how it can synchronize only part of the method if we locking whole the object ?

🧠 First, What Is a Monitor?

A monitor is a mutex (mutual exclusion lock) + condition variable attached to every object in Java.

  • Every Java object has one monitor.
  • A monitor ensures that only one thread at a time can hold the monitor lock on the object.
  • When a thread holds the monitor, other threads trying to lock it are blocked until it’s released.

🔐 So What Happens When You Synchronize?

🔸 synchronized(this) or synchronized void method()

  • Both mean: acquire the monitor lock on this object.
  • Once the thread has the monitor, it can enter the synchronized block or method.
  • Any other thread trying to acquire the same monitor will be blocked until it’s released.

🧩 Then… How Can Only Part of a Method Be Synchronized?

Let’s take this example:

public void doSomething() {
    // 🟢 Not synchronized
    System.out.println("Step 1 - open access");

    synchronized (this) {
        // 🔐 Only this part is synchronized
        System.out.println("Step 2 - critical section");
    }

    // 🟢 Not synchronized again
    System.out.println("Step 3 - open access again");
}

🧠 What’s Happening?

  • Only the code inside synchronized(this) is protected by the monitor on this
  • Code before and after the block executes freely by any thread
  • Multiple threads can run Step 1 and Step 3 in parallel
  • But only one thread can be inside Step 2 at a time, because it requires holding the monitor

📌 So yes — you’re locking the whole object, but only while you’re inside the synchronized block

🔍 So What Controls the Scope?

➡️ It’s the code, not the object.

Even though synchronized(this) locks the whole object, you’re not locking the object forever — you’re locking for the duration of that block only.

This is scoped synchronization:

  • You grab the monitor lock on this
  • You enter the block
  • You exit the block → monitor is automatically released

🔧 Visual Metaphor: Object as a Room With a Lock

  • The object has one door (monitor lock).
  • You (a thread) can enter a critical room (synchronized block) only if the door is unlocked.
  • You do your critical work inside — then leave and unlock the door.
  • But the object still exists and has other parts (unsynchronized code) outside the room that anyone can access.

💬 Summary

MythReality
Locking the object = locking the whole method❌ Not necessarily
synchronized(this) locks the whole object✅ But only for the duration of the block
You can’t control partial locking❌ You can! Use synchronized blocks
One monitor = all code is locked❌ Only code inside synchronized blocks/methods is guarded

Bonus: JVM Bytecode

When you write:

synchronized (obj) {
    // do something
}

The compiler generates:

monitorenter
// critical section
monitorexit

So you enter and exit the monitor only for that code block.

This entry was posted in Без рубрики. Bookmark the permalink.