The wait,notify of the Java Multithreading Foundation

Source: Internet
Author: User
Tags object object static class

Wait () and notify (), Notifyall () are present in the Object class, which means that the three methods must be called objecy.wait (), Object.notify ().

The premise of using these three methods is that the lock on the object must be obtained, that is, it must be executed in the Sychronize method.

The process of wait () is that when the thread executes object.wait (), the lock of the current object object is freed and the wait queue for the object is entered.

Notify () is performed when the thread executes object.notify (), wakes up a random thread in the wait queue for object, and Notifyall () is all the threads that wake object waiting for the queue.

We construct an instance: Two threads have synchronous operations on object objects, Thread 1wait (), Thread 2notigy () Wake thread 1, and view thread execution.

/******************************************************************************* *                   * COPYRIGHT (C) Tuniu limited-all rights RESERVED.                                                                                                                                  *                                                                               * Creation date:2016 Year September 27 * ************************************************************************ /package thread;import java.util.concurrent.timeunit;/** * @author ZHOUJIE8 * */public class Testwaitnofity {p    Rivate final Static Object object = new Object ();        public static void Main (string[] args) {Thread1 T1 = new Thread1 ();        Thread2 t2 = new Thread2 ();        T1.start ();    T2.start ();   } public static class Thread1 extends Thread {@Override     public void Run () {synchronized (object) {System.out.printf ("%s%s start\n", System.curre                Nttimemillis (), Thread.CurrentThread (). GetName ());                    try {System.out.printf ("%s%s wait\n", System.currenttimemillis (), Thread.CurrentThread (). GetName ());                Object.wait ();                } catch (Interruptedexception e) {e.printstacktrace ();            } System.out.printf ("%s%s end\n", System.currenttimemillis (), Thread.CurrentThread (). GetName ());            }}} public static class Thread2 extends Thread {@Override public void run () { Synchronized (object) {System.out.printf ("%s%s start\n", System.currenttimemillis (), Thread.CurrentThread                (). GetName ());                Object.notify ();                System.out.printf ("%s%s notify\n", System.currenttimemillis (), Thread.CurrentThread (). GetName ());     try {               TimeUnit.SECONDS.sleep (2);                } catch (Interruptedexception ex) {ex.printstacktrace (); }            }        }    }}
The output is:
1475030841975 Thread-0 start1475030842000 Thread-0 wait1475030842000 Thread-1 start1475030842000 Thread-1 notify1475030844001 Thread-1 end1475030844001 Thread-0 Wait

It can be seen that after wait (), THREAD0 releases the lock of object and enters the wait queue, Thread1 obtains the lock, and wakes the Thread0.

However, it should be noted that Thread-1 sleep does not release the lock of object within 2S, so Thread-0 is awakened after 2S and acquires an object lock to continue execution.

The wait,notify of the Java Multithreading Foundation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.