Java thread wait, policy, and policyall

Source: Internet
Author: User

Wait (), Policy (), and policyall () are the three local methods of the object class. They are all final and cannot be overwritten, that is, all objects contain these three methods. In general, the wait () method is to release the lock on this object by the thread that calls this method and cause the current thread to block. The notify () method randomly selects a thread that is blocked because the wait () method is called. The yyall () method is used to wake up all threads blocked by calling the wait () method, but only one thread can obtain the thread lock.

Note: The thread operations involved here are all the same object, otherwise it will be meaningless.

(1) Wait ()

1. When calling this method, the current thread must own the object lock.

2. The current thread releases the lock and blocks it.

3. The current thread will be congested until the thread gets the object lock again and resumes execution. To resume execution of the current thread, another thread needs to call the notify () or yyall () method to notify the thread waiting for the lock to resume execution (not necessarily because the current thread will recover ).

4. This method is always used in a loop.

 
Synchronized (OBJ) {While (<condition does not hold>) obj. Wait ();... // perform action appropriate to condition}

5. This method should be called by a thread with an object lock; otherwise, an exception is thrown.

(2) notify ()
1. Wake up a thread waiting for the current object lock. If multiple threads are waiting for the current object lock, only one thread will be awakened.

2. The wake-up thread does not execute immediately until the current thread releases the lock for this object.

3. This method should be called by a thread with an object lock; otherwise, an exception is thrown.

(3) policyall ()

1. Wake up all threads waiting for the current object lock, which is different from running y.

2. The wake-up thread does not execute immediately until the current thread releases the lock for this object. At the same time, all the wake-up threads will compete fairly, get the object lock and execute it.

3. This method should be called by a thread with an object lock; otherwise, an exception is thrown.

Note:

1. When calling these three methods, the thread must have a lock on the object; otherwise, an illegalmonitorstateexception will be thrown. Generally, it is in the synchronized method or block that the thread will own the lock.

2. After the notify () and notifyall () methods are called, the wake-up thread will not be executed immediately, but will not be executed until the thread that owns the object lock releases the lock.

3. The major difference between notify () and notifyall () Is that notify () will wake up only one thread no matter how many threads are blocked. Other blocked threads will not be woken up, only the wake-up thread can be executed. Other blocked threads will continue to be blocked. policyall () will wake up all blocked threads, and these threads will execute (the premise is to obtain the object lock ).


Example:

Package test. thread; import Java. util. arraylist; import Java. util. list;/*** simple producer and consumption simulation **/public class test {public static void main (string [] ARGs) {final bean = new bean (); new thread (New runnable () {public void run () {for (INT I = 0; I <50; I ++) bean. create ();}}). start (); For (INT I = 0; I <50; I ++) bean. use () ;}} class bean {private list <string> List = new arraylist <string> ();/*** production **/publi C synchronized void create () {While (list. Size ()! = 0) {try {This. wait ();} catch (interruptedexception e) {e. printstacktrace () ;}}for (INT I = 0; I <10; I ++) {system. out. println ("create Bean:" + I); list. add ("Bean:" + I);} This. Y ();}/*** consumption **/Public synchronized void use () {While (list. size ()! = 10) {try {This. wait ();} catch (interruptedexception e) {e. printstacktrace () ;}} while (list. size ()> 0) {system. out. println (list. remove (0);} This. notify ();}}

Another example that is easy to create a deadlock is as follows:

 
Package test. thread; import Java. util. using list; import Java. util. concurrent. executorservice; import Java. util. concurrent. executors; public class Stack {synchronized list = new synchronized list (); Public synchronized void push (Object X) {synchronized (list) {list. addlast (x); y () ;}} public synchronized object POP () throws exception {synchronized (list) {If (list. isempty () {Wait ();} return list. removefirst () ;}} public static void main (string [] ARGs) {final stack S = new stack (); executorservice exec = executors.newcachedthreadpool(new runnable exec.exe cute (New runnable () {public void run () {try {system. out. println (S. pop ();} catch (exception e) e.printstacktrace({{}}}{exec.exe cute (New runnable () {public void run () {S. push ("test") ;}}); Exec. shutdown ();}}

In the preceding example, when the wait () and notify () methods are called, the lock of the stack object is released, but the lock on the shortlist object may not be released.ProgramCompeting with the lock on the shortlist object causes a deadlock.

 

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.