Does javascript sync automatically notify on exit? Is this expected?

Java Question:

Exiting the synchronization block automatically notifies All (). Is this expected behavior?

I tested it, and it seems to me 1. When the execution exits the synchronization block, it automatically notifies All () 2. When the method itself is synchronized, it automatically notifies () when it returns. (Not notifyAll ())

The code:

 public class Test { public static void main(String[] args) throws InterruptedException { MyThread lock = new MyThread(); new WatingThread(lock,1).start(); new WatingThread(lock,2).start(); //above tow threads would start and then wait for a lock lock.start(); } } class MyThread extends Thread { public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("MyThread is trying to acquire the lock .."); synchronized (this) { System.out.println("MyThread has acquired the lock !!"); System.out.println("MyThread is Coming out of synch block.."); } System.out.println("MyThread has released the lock !!"); } } class WatingThread extends Thread { private Object lock; private int id; public WatingThread(Object lock, int id ) { this.lock = lock; this.id = id; } @Override public void run() { System.out.println(String.format("[%d] : Check if lock is available ...",new Object[]{id})); synchronized (lock) { System.out.println(String.format("[%d] : Acquired the lock !!",new Object[]{id})); try { System.out.println(String.format("[%d] : Going to wait on lock.. ",new Object[]{id})); lock.wait(); System.out.println(String.format("[%d] : Got notified !!!",new Object[]{id})); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(String.format("[%d] :I am done !!",new Object[]{id})); } } } 

code>

Conclusion:

[2]: check if the lock is free ...
[2]: I got a castle!
[1]: Check if the lock is free ...
[2]: Wait until it is locked.
[1]: I got a castle!
[1]: Waiting for a lock.
MyThread is trying to get a lock.
MyThread has acquired a castle!
MyThread exits the sync block.
MyThread has released a lock!
[1]: Received a notification !!!
[1]: I am done !!
[2]: Received a notification !!!
[2]: I am done !!

+4
source share
2 answers

What you discovered is that java.lang.Thread uses a wait object that internally uses itself as a lock. This is described in the description of the Thread.join() method (not the best place, perhaps):

This implementation uses the this.wait call loop, driven by this.isAlive . When the thread completes, this.notifyAll method this.notifyAll called. It is recommended that applications do not use wait , notify or notifyAll for Thread instances.

By the way, if you used a while loop to check whether the waiting condition has changed in accordance with best practice, this will protect you from such awakenings, although, of course, it is best to use Object as a lock anyway.

+5
source

You should not use the Thread object as a lock object. See Additional Explanations Explicitly notifies java of thread expectations? .

Advertising: Jon Skeet says there :) (This is a direct link to the answer in the above related question.)

There is another question related to the comment, now I will make it more reachable: who and when notifies thread.wait () when thread.join () is called?

+4
source

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


All Articles