Java multithreading and concurrent applications-traditional thread communication technology questions

Source: Internet
Author: User

Java multithreading and concurrent applications-traditional thread communication technology questions

Package com. lipeng; public class LoopDemo {/*** thread A loops 10 times, then thread B loops 100 times, then A recycles 10 times, and B recycles 100 times. This loops 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, ""). start (); // The start method of each thread can only Run 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; // switch. When true, A is executed, and false is executed. After execution, the switch condition is public synchronized void () throws Exception {while (! AGo) // Why not use if ??? {// A cannot run. wait for this. wait () ;}for (int I = 0; I <10; I ++) {System. out. println ("AAAAA --------------" + I);} // after running, switch back to FALSE to allow B to run aGo = false; this. Y (); // wake up a waiting thread} public synchronized void B () throws Exception {while (aGo) {// B cannot run, wait this. wait () ;}for (int I = 0; I <100; I ++) {System. out. println ("B --------------" + I);} // after running, switch back to true to allow A to run aGo = true; this. Y (); // wake up a waiting thread }}


Note:

1. If there are more than two multithreading in execution, it is best to use policyall. Why?

For example, if there are several more threads in this example, if there are A1 and B1 threads in wait at this time and A thread A finishes executing, set aGo to false, and wake up one of the threads waiting for the lock, A1 or B1. If the A1 thread grabs the CPU at this time, A1 runs and judges that method A1 enters the wait status, because B1 is not awakened and continues to wait, both A1 and B1 are in the wait Status at the end, resulting in a deadlock.

If changed to policyall, both A1 and B1 will be awakened. The thread that grabs the lock will execute first. If A1 grabs the lock, it will continue to wait. Then B1 continues to execute because aGo = false at this time, therefore, when B1 is executed, the values aGo = true and A1 are executed successfully.

Conclusion: as long as the number of threads started by A and B is different, A deadlock may occur.

2. Judge why the while clause is used in the place of aGo. if not, why? In the description of the wait method, we also recommend that you use while, because in some specific cases, the thread may be falsely awakened, and the loop detection with while is more secure. In this example, A and B should run alternately, but if several threads are started more, if A1 thread is waiting (wait), and A2 thread is finished, aGo is set to false to wake up the waiting thread (maybe A or B). if A is woken up and if rather than while is used, A executes, in this way, A is executed twice consecutively and does not meet our design requirements. However, if A is used for while, even if A is awakened, He will judge the value of aGo again and find that it is false, then wait. Until the B thread wakes up.

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.