0

I'm bit confused in lock mechanism in synchronized block.

When ever a Thread tries to lock a synchronized block, it looks for available key (could be a class level or instance level) to have a lock on synchronized block.

I want to know what's that specific instance which tells specific thread that the key is available ? Who plays the receptionist role to acknowledge the thread request to get a key so that it could have lock on synchronized block?

Can anyone share any simple explanation with some example ?

user207421
  • 305,947
  • 44
  • 307
  • 483
Jenny
  • 47
  • 6
  • The simple answer is: the JVM does it, and you don't need to worry about how it's done (much like the way you don't need to worry about how memory is allocated or deallocated). You can check if the current thread holds a lock in your code, using [`Thread.holdsLock`](https://docs.oracle.com/javase/8/docs/api/java/lang/Thread.html#holdsLock-java.lang.Object-), but it is *rare* to need to do this. – Andy Turner Sep 07 '20 at 11:21
  • Your question seems to be based on a misunderstanding of locks in Java. They don't have "keys". It is not a good idea to try to understand the meaning of an IT term by trying to draw analogies with how the word is used in a non-IT context. (I can give you many examples where an IT term means something completely different to the non-IT word. And in this case: https://en.wikipedia.org/wiki/Lock) – Stephen C Sep 07 '20 at 11:27
  • @MarquisofLorne, what's that thing which helps synchronized block to attend one thread a time ? There must be something working behind synchronized block which keep the upcomiong thread on hold to proceed further until current execution is completed ? – Jenny Sep 07 '20 at 13:12

1 Answers1

1

There is no receptionist, it works more like a bathroom: Everyone locks the door upon entering, and people don't try to enter the bathroom if the door is locked:

while door is locked
    wait
lock the door
do your business
unlock the door

That works provided that only a single person can succeed in locking the door (i.e. we check that we are alone in the bathroom before lowering our pants).

In IT terms, the JVM does something like this:

class Monitor {
    AtomicReference owner = new AtomicReference();

    void lock() {
        do {
            boolean alone = owner.compareAndSet(null, Thread.currentThread());

            if (alone) {
                return;
            } 

            wait();
        } while (true);
    }

    void unlock() {
        if (owner.compareAndSet(Thread.currentThread(), null)) {
            return;
        } else {
            throw new IllegalMonitorStateException();
        }
    }
}    

AtomicReference.compareAndSet delegates to a special hardware instruction that only updates a memory location if the previous value is as expected. The hardware ensures that this instruction is atomic, i.e. it is not possible for another instruction to change this memory location while this instruction executes. The particulars of the hardware instruction used depend on which hardware the JVM is running on.

meriton
  • 68,356
  • 14
  • 108
  • 175