A stream containing multiple locks goes into wait () state. Will all locks be unlocked?

I wrote this program to check if thread t1 is blocked on two different objects: Lock.class and MyThread.class goes into standby mode of an instance of MyThread.class using MyThread.class.wait (). It does not release the lock on the Lock.class instance. why is that? I thought that when a thread goes into standby mode or it dies, it releases all acquired locks.

public class Lock { protected static volatile boolean STOP = true; public static void main(String[] args) throws InterruptedException { MyThread myThread = new MyThread(); Thread t1 = new Thread(myThread); t1.start(); while(STOP){ } System.out.println("After while loop"); /* * */ Thread.sleep(1000*60*2); /* * Main thread should be Blocked. */ System.out.println("now calling Check()-> perhaps i would be blocked. t1 is holding lock on class instance."); check(); } public static synchronized void check(){ System.out.println("inside Lock.check()"); String threadName = Thread.currentThread().getName(); System.out.println("inside Lock.Check() method : CurrrentThreadName : "+ threadName); } } class MyThread implements Runnable{ public MyThread() { } @Override public void run() { try { System.out.println("inside Mythread run()"); classLocking(); } catch (InterruptedException e) { e.printStackTrace(); } } public static synchronized void classLocking() throws InterruptedException{ System.out.println("inside Mythread.classLocking()"); String threadName = Thread.currentThread().getName(); System.out.println("inside MyThread.classLocking() : CurrrentThreadName : "+ threadName); /* * outer class locking */ synchronized (Lock.class) { System.out.println("I got lock on Lock.class definition"); Lock.STOP = false; /* * Outer class lock is not released. Lock on MyThread.class instance is released. */ MyThread.class.wait(); } } } 
+6
source share
4 answers

You are correct that it does not release another lock. As for the reason, because it is unsafe. If it would be safe to release the external lock while calling the internal function, why would the internal function be called with the rest of the lock?

After the function exits, a lock that was not obtained behind the back of the programmer will destroy the logic of the synchronized functions.

+4
source

The semantics of wait () is that the calling thread pays attention to the fact that the lock has already been received by another thread, pauses and waits for a notification from the thread holding the lock when the latter releases it (and calls the notification), This does not mean that when pending it releases all acquired locks. You can see the waiting challenges as a series of obstacles that the thread faces on the path to acquiring all the locks needed to complete the action.

Regarding the question β€œWhy does a thread not release all the locks received by calling wait”, I think the answer is that it will make it more prone to hunger, and it will also slow down progress in a multi-threaded application (all threads will discard all their locks on first call and will have to start when they get the lock they are currently waiting for, so they will be in constant battle for locks. In fact, in such a system, the only thread that can complete execution e, there will be one that can find all the locks for free when they need it. This is unlikely to happen)

0
source

From the JavaDoc of the wait() method

The current thread must own this object. The thread frees the owner of this monitor and waits until the other thread notifies the threads waiting on this object monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-acquire ownership of the monitor and resume execution.

0
source

Yes, it works correctly. The thread goes into the idle state releases the corresponding lock, and not all locks. Otherwise, think: if everything is like what you thought, then when the thread waits, it loses all acquired locks, which makes it impossible to continue sequential execution.

0
source

Source: https://habr.com/ru/post/920538/


All Articles