Java Thread Wait, notify, and Notifyall

Source: Internet
Author: User

The Java object class contains three final methods that allow threads to communicate about the lock state of a resource. These three methods are: Wait (), notify (), Notifyall (), today to learn about these three methods. The current thread that calls these methods on any object should have an object monitor (lock an object, which is to get the monitor associated with the object), or throw a java.lang.IllegalMonitorStateException exception.

wait

Object.wait has three overloaded implementations, one that waits indefinitely for any other thread to invoke the Notify or Notifyall method of the object to wake the current thread. The other two causes the current thread to wake up after a specific time has been awaited.

Wait ()

Causes the current thread process to wait until another thread calls the Notify () method or the Notifyall () method on the object. The behavior of this method is exactly equivalent to calling wait (0), which can be seen by its implementation code:

     Public Final void throws interruptedexception {        Wait (0);    }

The current thread must obtain the monitor for this object. The thread releases the ownership of the monitor until another thread calls the Notify method or the Notifyall method to wake up other threads waiting on the object monitor. When ownership of the monitor is released, the thread waits until the monitor's ownership is regained and execution resumes. A thread in the wait state may be interrupted or falsely awakened, so this method should always be used in a loop:

         synchronized (obj) {              while (<condition does not hold>)                  obj.wait ();                // Perform action appropriate to condition          }

False awakening refers to some obj.wait () that are awakened in other cases except obj.notify () and Obj.notifyall (), which should not be awakened at this time, and in more detail can be seen spurious_wakeup.

Method throws two kinds of exceptions:

illegalmonitorstateexception: If the current thread does not have a monitor for the current object.

interruptedexception: If a thread waits for notification at the current thread, or interrupts the current thread before waiting for notification, the current thread's interrupt state is cleared when the exception is thrown.

Wait (long timeout)

This method causes the current process to enter the wait state until another thread calls Notify/notifyall, or the thread is awakened after a specified time. The method throws an exception:

illegalargumentexception: If timeout is a negative number.

Wait (long timeout, int nanos)

Similarly, this method causes the current process to enter the wait state until another thread calls Notify/notifyall. It can control the waiting time more finely, the real-time amount measured in nanoseconds is given by the following formula: 1000000*timeout+nanos. In addition, this method does the same thing as wait (long), in particular, wait (0,0) is equivalent to wait (0). This method throws an exception except for the exception that is thrown by the remaining two wait methods:

illegalargumentexception: This exception is thrown if timeout is a negative number, or if the range of Nanos is not between 0-999999.

Notify

The Notify method wakes only one thread of the waiting object, and the thread begins execution. So if there are multiple threads waiting for an object, this method will only awaken one of them. The choice of thread depends on the OS implementation of thread management.

Notifyall

The Notifyall method wakes up all the threads waiting on the object, but which one will be processed first depends on the implementation of the operating system.

These methods can be used to implement producer consumer issues where a consumer thread is waiting for an object in the queue, and the producer thread puts the object in the queue and notifies the waiting thread. Here is an example of multiple threads working on the same object, using the Wait,notify,notifyall method:

An exampleMessage: Object awakened by Notification
 Public class Message {    private  String msg;      Public Message (String str) {        this. msg=str;    }       Public String getmsg () {        return  msg;    }      Public void setmsg (String str) {        this. msg=str;    }} 
Notifier: Performing a wake-up operation
 Public classNotifierImplementsRunnable {PrivateMessage msg;  PublicNotifier (Message msg) { This. msg =msg; } @Override Public voidrun () {String name=Thread.CurrentThread (). GetName (); SYSTEM.OUT.PRINTLN (Name+ "Started"); Try{Thread.Sleep (1000); synchronized(msg) {msg.setmsg (name+ "Notifier work done"); Msg.notify ();//Msg.notifyall ();            }        } Catch(interruptedexception e) {e.printstacktrace (); }    }}
Waiter: Make Message object wait state
 Public classWaiterImplementsrunnable{PrivateMessage msg;  PublicWaiter (Message m) { This. msg=m; } @Override Public voidrun () {String name=Thread.CurrentThread (). GetName (); synchronized(msg) {Try{System.out.println (name+ "Waiting to get notified at time:" +System.currenttimemillis ());            Msg.wait (); }Catch(interruptedexception e) {e.printstacktrace (); } System.out.println (Name+ "Waiter thread got notified at time:" +System.currenttimemillis ()); //Process the message nowSYSTEM.OUT.PRINTLN (name+ "processed:" +msg.getmsg ()); }    }}
Waitnotifytest: Testing
 Public classWaitnotifytest { Public Static voidMain (string[] args) {Message msg=NewMessage ("Process it"); Waiter Waiter=NewWaiter (msg); NewThread (Waiter, "Waiter"). Start (); Waiter Waiter1=NewWaiter (msg); NewThread (Waiter1, "Waiter1"). Start (); Notifier Notifier=NewNotifier (msg); NewThread (notifier, "notifier"). Start (); System.out.println ("All the threads is started"); }}

Output:

Waiter waiting to get notified @ time:1516757290631 all thethreads is startedwaiter1 waiting to get notified at time :1516757290632notifier startedwaiter Waiter thread got notifiedat time: 1516757291632Waiter Processed:notifier notifier work done

You can see two threads waiting on the object msg, and when the Notify method is called, only one thread is awakened, and the program does not exit because there is still a thread waiting.

If the Notify method is changed to Notifyall, the result of the operation is:

Waiter waiting to get notified @ time:1516757437164Waiter1 waiting to get notifiedon time: 1516757437164 all The threads is startednotifier startedwaiter1 Waiter thread got notifiedat time: 1516757438165waiter1 processed : notifier notifier work Donewaiter Waiter thread got notifiedat time: 1516757438165Waiter Processed:notifier No Tifier work done

You can see that two threads have been woken up and the program is out of operation.

Finish

Java Thread Wait, notify, and Notifyall

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.