Communication between Java threads

Source: Internet
Author: User

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

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.