Multithreading - wait (Wait) and notification (notify)

1. To support collaboration between multiple threads, JDK provides two very important thread interfaces: wait wait () method and notification notify () method.

   These two methods are not in the Thread class, but the output in the Object class. This means that any object can call these two methods.

 These two methods are as follows  

  

  

 

 When you call wait on an object instance () method, the current thread will wait on this object.

 For example, the thread A, called obj.wait () method, the thread A will stop execution continues into a wait state. Wait for it to when it ends? A thread will wait until the other thread calls obj.notity () method so far.

2.wait () method, and notify () method exactly how to work it?

 

 

   If a thread calls object.wait () method, then it will enter the waiting queue object object, this queue, there may be multiple threads to run multiple threads at the same time as the system waits for an object,

  When object.notify () method is called, it will be selected at random from the queue waiting for a thread, and wake up.

  In addition to notity () method, Object object has a similar notifyAll () method, and it's basically the same function notity method, except that it will wake up in the waiting queue all waiting threads, rather than a random.

  object.wait () method does not just call. It must be included in synchronzied statement object, either wait () method or notity () method needs to first obtain a monitor target object.

 

 

 

 

   As shown, wherein T1 and T2 represent two threads, T1 right before the execution wait () method, the object must be a monitor object, and then wait () method releases the execution monitor.

The aim is to make the other waiting on the object because the object thread T1 will not sleep and can not perform all normal.

  Thread T2 in notity () method is called before, it must be a monitor target object. At this time T1 has released the monitor. Therefore, to secure the object T2 may monitor object.

Subsequently, T2 perform the notify () method attempts to wake up a waiting thread, assuming a wake-T1, T1 is the wake-up, the first thing to do subsequent code is not executed, but the attempt to reactivate

Obtaining object object's monitor, and this monitor is also executed before T1 held that in wait () method.

If temporarily unavailable, you must wait for the T1 monitor. After successfully obtaining a monitor, T1 can continue to perform in the true sense.

 

3. In order to facilitate understanding, a simple case:

/**
 * wait和 notify 的学习
 * @author wm
 * @date 2019/9/18 15:15
 */
public class SimpleWN {
    final static Object object = new Object();

    public static class T1 extends  Thread{
        public void run() {
            synchronized (object){
                System.out.println(System.currentTimeMillis()+":T1 start! ");
                System.out.println(System.currentTimeMillis()+":T1 wait for object");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(System.currentTimeMillis()+":T1 end! ");
            }
        }
    }

    public static class T2 extends Thread{
        public void run() {
            synchronized (object){
                System.out.println(System.currentTimeMillis()+":T2 start! notify one thread");
                object.notify();
                System.out.println(System.currentTimeMillis()+":T2 end! ");
                try {
                    object.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        Thread t1 = new T1();
        Thread t2 = new T2();
        t1.start();
        t2.start();
    }
}

 The above code, the opened two threads T1 and T2. T1 executed Object.wait () method. Execution wait () method before, T1 is the first application object lock object.

Therefore, when performing object.wait (), which is held by the object lock object. After the wait () method performs, Tl will wait, and release the object lock object.

T2 before performing notity () method will first obtain the object lock object, to make this clear result, specially after notity () method is performed, so that the sleep T2 2 seconds,

Doing so may be more clearly explained, T1 after obtaining notity () method of notification, or will attempt to regain the object lock object.

Results of the:

1570675715571:T1 start! 
1570675715571:T1 wait for object
1570675715571:T2 start! notify one thread
1570675715571:T2 end! 
1570675715571:T1 end! 

After T2 T1 notice continues, T1 and can not proceed immediately, but to wait for the release of the lock object of T2, and again after successfully lock to continue;

 

4.Object.wait () method and Thread.sleep () method can make a thread wait for some time, except wait () method can be awakened outside,

Another major difference is the wait () method will release the lock of the target object, and Thread.sleep () method does not release any resources.

 

Guess you like

Origin www.cnblogs.com/SuperManer/p/11645976.html