Tag: Get an Exchange await code ace TAC Div Communication
1. Common methods
Sleep () The thread goes into a wait state and does not release the lock
Wait () The thread enters the waiting state, releasing the lock
Notify () Random wake-up of a thread
Notifyall () Wake Up all threads
2. Communication between threads
A. Communication between two threads
public class Threadexchange {@Testpublic void Test2thread () {myprint myprint = new Myprint (); New Thread (New Runnable () {@O verridepublic void Run () {while (true) {try {Myprint.printa ()} catch (Interruptedexception e) {e.printstacktrace ();}}}) . Start (); New Thread (New Runnable () {@Overridepublic void run () {while (true) {try {myprint.printb ()} catch (Interruptede Xception e) {e.printstacktrace ();}}}). Start ();}} Class Myprint {private Integer flag = 0;public synchronized void PrintA () throws Interruptedexception {if (flag! = 0) {thi S.wait ();} System.out.print ("a"); System.out.print ("a"); System.out.print ("a"); System.out.print ("a"); System.out.println ("a"); flag = 1;this.notify ();} Public synchronized void Printb () throws Interruptedexception {if (flag! = 1) {this.wait ();} System.out.print ("B"); System.out.print ("B"); System.out.print ("B"); System.out.print ("B"); System.out.println ("B"); flag = 0;this.notify ();}}
B. Communication between more than three threads
Way One
Public classThreadexchange {@Test Public voidTest2thread () {myprint myprint=NewMyprint (); NewThread (NewRunnable () {@Override Public voidrun () { while(true) { Try{Myprint.printa (); } Catch(interruptedexception e) {e.printstacktrace (); }}}). Start (); NewThread (NewRunnable () {@Override Public voidrun () { while(true) { Try{MYPRINT.PRINTB (); } Catch(interruptedexception e) {e.printstacktrace (); }}}). Start (); }}classMyprint {PrivateInteger flag = 0; Public synchronized voidPrintA ()throwsinterruptedexception {if(Flag! = 0) { This. Wait (); } System.out.print (A); System.out.print (A); System.out.print (A); System.out.print (A); System.out.println (A); Flag= 1; This. Notify (); } Public synchronized voidPRINTB ()throwsinterruptedexception {if(Flag! = 1) { This. Wait (); } System.out.print ("B"); System.out.print ("B"); System.out.print ("B"); System.out.print ("B"); System.out.println ("B"); Flag= 0; This. Notify (); }} 2. Communication between more than three threads Public classThreadexchange {@Test Public voidTest2thread () {FinalMyprint p =NewMyprint (); NewThread () { Public voidrun () { while(true) { Try{p.print1 (); } Catch(interruptedexception e) {e.printstacktrace (); }}}}.start (); NewThread () { Public voidrun () { while(true) { Try{p.print2 (); } Catch(interruptedexception e) {e.printstacktrace (); }}}}.start (); NewThread () { Public voidrun () { while(true) { Try{p.print3 (); } Catch(interruptedexception e) {e.printstacktrace (); }}}}.start (); }}classMyprint {Private intFlag = 1; Public voidPrint1 ()throwsinterruptedexception {synchronized( This) { while(Flag! = 1) { This. Wait (); } System.out.print (A); System.out.print (A); System.out.print (A); System.out.print (A); System.out.println (A); Flag= 2; This. Notifyall (); } } Public voidPrint2 ()throwsinterruptedexception {synchronized( This) { while(Flag! = 2) { This. Wait (); } System.out.print ("B"); System.out.print ("B"); System.out.print ("B"); System.out.print ("B"); System.out.println ("B"); Flag= 3; This. Notifyall (); } } Public voidPrint3 ()throwsinterruptedexception {synchronized( This) { while(Flag! = 3) { This. Wait (); } System.out.print (C); System.out.print (C); System.out.print (C); System.out.print (C); System.out.println (C); Flag= 1; This. Notifyall (); } }}
Way Two
Public classThreadexchange {@Test Public voidTest3thread () {FinalMyprint p =NewMyprint (); NewThread () { Public voidrun () { while(true) { Try{p.print1 (); } Catch(interruptedexception e) {e.printstacktrace (); }}}}.start (); NewThread () { Public voidrun () { while(true) { Try{p.print2 (); } Catch(interruptedexception e) {e.printstacktrace (); }}}}.start (); NewThread () { Public voidrun () { while(true) { Try{p.print3 (); } Catch(interruptedexception e) {e.printstacktrace (); }}}}.start (); }}classMyprint {PrivateReentrantlock r =NewReentrantlock (); PrivateCondition C1 =r.newcondition (); PrivateCondition C2 =r.newcondition (); PrivateCondition C3 =r.newcondition (); Private intFlag = 1; Public voidPrint1 ()throwsinterruptedexception {r.lock ();//Get lock if(Flag! = 1) {c1.await (); } System.out.print (A); System.out.print (A); System.out.print (A); System.out.print (A); System.out.println (A); Flag= 2; C2.signal (); R.unlock (); //Release Lock } Public voidPrint2 ()throwsinterruptedexception {r.lock (); if(Flag! = 2) {c2.await (); } System.out.print ("B"); System.out.print ("B"); System.out.print ("B"); System.out.print ("B"); System.out.println ("B"); Flag= 3; C3.signal (); R.unlock (); } Public voidPrint3 ()throwsinterruptedexception {r.lock (); if(Flag! = 3) {c3.await (); } System.out.print (C); System.out.print (C); System.out.print (C); System.out.print (C); System.out.println (C); Flag= 1; C1.signal (); R.unlock (); }}
Communication between Java threads