Java Multi-Threading and concurrency Applications-(4)-Traditional thread communication technical questions

Source: Internet
Author: User

Package Com.lipeng;public class Loopdemo {/** * thread a loops 10 times, then thread B loops 100 times, then a loops 10 times, then B loops 100 more times. So Loop 50 times. * Lipeng * 2015-4-10 * @param args */public static void main (string[] args) {mytask task=new mytask (); RunA runa=new RunA (Task); Runb runb=new Runb (Task); new Thread (RunA, "A"). Start ();  The Start method for each thread runs only once, but the Run method can run multiple times in different threads. New Thread (Runb, "B"). Start ();}}

Class RunA implements Runnable{private mytask task;public RunA (mytask Task) {this.task=task;} @Overridepublic void Run () {try {for (int i=0;i<50;i++) {task. A ();}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}


Class Runb implements Runnable{private mytask task;public  Runb (mytask Task) {this.task=task;} @Overridepublic void Run () {try {for (int i=0;i<50;i++) {task. B ();}} catch (Exception e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}

Class Mytask{private Boolean ago=true;//toggle switch, when True a executes, false when B executes, after execution switches the condition public synchronized void A () throws Exception {while (!ago)  //Why not use the IF??? {//a cannot run, wait for this.wait ();} for (int i=0;i<10;i++) {System.out.println ("AAAAA--------------" +i); After running, switch ago to false, let B run ago=false;this.notify ();//Wake up a waiting thread}public synchronized void B () throws Exception{while ( AGo) {//b cannot run, wait for this.wait ();} for (int i=0;i<100;i++) {System.out.println ("B--------------" +i);} After running, switch ago to true, let a run ago=true;this.notify ();//Wake up a waiting thread   }}


Description:

1. If there are more than two multi-threading in execution, it is best to use Notifyall, why?

For example, if there are several threads in this example, if there is a a1,b1 thread waiting at this time, a thread of a is finished, it will be set to false before, and wake up one of the threads that are waiting for the lock, A1 or B1, if the A1 thread grabs the CPU at this point, then A1 runs, The Judgment method A1 enters the wait state, and B1 is not awakened, then continues to wait, then finally, A1 and B1 are in the wait state, resulting in a deadlock.

and if you change to Notifyall, A1 and B1 are awakened, and the thread that grabs the lock executes first, if the A1 grab, continue to wait, and then B1 continue to execute, because at this time ago=false, so B1 execution, will ago=true,a1 continue execution completed.

Conclusion: As long as the number of threads initiated by A and B are different, a deadlock can occur.

2. Why do I have to use while,if before the place of the ago? While in the Wait method description, it is also recommended to use while, because in some specific cases it is possible for a thread to be falsely awakened, and a while loop to detect more reliably. In this case, A and B should be run alternately, but if you start several threads more, if there is a A1 thread waiting (wait), and the A2 thread finishes executing, the ago setting is false, waking the waiting thread (possibly a or b), and if A is awakened, If you use if instead of while, then a executes, so that a is executed two times, does not conform to our design requirements, but if the use of while, even if a is awakened, he will again judge the value of the ago, and found that is false, continue to wait. Until the b thread wakes it up.

Java Multi-Threading and concurrency Applications-(4)-Traditional thread communication technical questions

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.