Java multi-thread wait () and notify ()

Source: Internet
Author: User

Let's take a look at the test code. For details, see the notes.

Package COM. jadyer. thread. wait;/*** Java multithreading: Wait () and Policy () * @ see ================================================= ========================================================== ================================================================= *@ see problems: start two threads at the same time and start four threads at the same time. When the console prints a different result * @ see starts two threads at the same time, when the console regularly outputs 1010101010101010 * @ see and starts four threads at the same time, the console regularly outputs 10101010 at first. Once a negative number is output at a certain moment, then the subsequent output will be "one error and one error" * @ see analysis: For the thread, any situation is reasonable * @ see assume one of the cases here: execute tt22 first. At this time, number = 0, Therefore, the wait () method in the decrease () method is executed, so tt22 is blocked * @ see and then tt44 is executed. At this time, number = 0, so decrease () is also executed () the wait () method in the method, so tt44 is blocked * @ see and then tt11 is executed. At this time, number = 0, and then the increase () method is executed () the number ++ and notify () methods in the Method * @ See Focus On The tt11 execution to the notify () method. We assume that this method wakes up the tt44 thread, so tt44 starts to execute decrease () the number in the Method -- * @ see at this time, number =-1, and then runs the notify () method in the decrease () method. We also assume that the notify () method () the method is to wake up the tt22 thread * @ see in the same way. The number is reduced again, so the number =-2 is printed to the console, and then assumed that the worker y () The method wakes up the cycle of tt11 * @ see, then we can see the effect of "one error and one error" * @ see ========================== ========================================================== ========================================================== =====* @ see: we should judge again when wait () is awakened, and then decide whether to let the thread continue wait () * @ see because when a thread is awakened, it does not know that a trojan occurs during its sleep, so it must be judged again. So change if () to while () to judge, you can * @ see =============================================== ========================================================== =================================================================* @ see supplement: if there are only two threads, one is the thread that increases the number, and the other is the thread that decreases the number. At this time, if () is used () there is no problem with determination * @ see because no matter how the thread wakes up, it wakes up another thread, the third thread is not inserted into the mess * @ see ============================ ========================================================== ========================================================== =====* @ author macro Yu * @ Re Ate Feb 22,201 2 3:20:05 */public class waitpolicytest {public static void main (string [] ARGs) {count = new count (); thread tt11 = new thread (New increasethread (count); thread tt22 = new thread (New decreasethread (count); thread tt33 = new thread (New increasethread (count )); thread tt44 = new thread (New decreasethread (count); tt11.start (); tt22.start (); tt33.start (); tt44.start ();} class increas ETHREAD implements runnable {private count; Public increasethread (count) {This. count = count ;}@ overridepublic void run () {for (INT I = 0; I <20; I ++) {try {thread. sleep (long) (math. random () * 1000);} catch (interruptedexception e) {e. printstacktrace ();} count. increase () ;}} class decreasethread implements runnable {private count; Public decreasethread (count) {This. count = count;} @ overrid Epublic void run () {for (INT I = 0; I <20; I ++) {try {thread. sleep (long) (math. random () * 1000);} catch (interruptedexception e) {e. printstacktrace ();} count. decrease () ;}} class count {private int number; Public synchronized void increase () {If (0! = Number) {try {// In the synchronous method (or synchronous statement block), the locked object can call the wait () method, this will cause the current thread to be blocked and release the mutex lock of the object // that is, the lock status of the current object corresponding to the wait () method is removed. Then, other threads have the opportunity to access this object wait ();} catch (interruptedexception e) {e. printstacktrace () ;}} number ++; system. out. println (number); // wake up other threads waiting for the same object because the wait () method is called. // This method can only wake up one thread in the waiting queue at a time, the thread scheduler determines which thread is awakened, and the programmer cannot control the thread y ();} public synchronized void decrease () {If (0 = number) {try {Wait ();} catch (interruptedexception e) {e. printstacktrace () ;}} number --; system. out. println (number); Policy ();}}

Related Article

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.