Java Multithreaded Communication Example

Source: Internet
Author: User

1  PackageCom.cl.www.thread;2 3  Public classNumberholder {4     5     PrivateInteger Number = 0;6     7     //Add number8      Public synchronized voidIncreasenum () {9         //No, not 0 .Ten          while(Number! = 0){    One             Try { A                  This. Wait (); -}Catch(interruptedexception e) { - e.printstacktrace (); the             } -         } -number++; -System.out.println (the value of "number is:" +Number ); +          This. Notifyall ();//Notify Other Threads -     } +      A     //Reduce number at      Public synchronized voidDecreasenum () { -         //if it equals 0, it doesn't decrease . -          while(Number = = 0){ -             Try { -                  This. Wait (); -}Catch(interruptedexception e) { in e.printstacktrace (); -             } to         } +number--; -System.out.println (the value of "number is:" +Number ); the          This. Notifyall ();//Notify Other Threads *     } $}

1  PackageCom.cl.www.thread;2 /**3  * 4 * Add number5  */6  Public classIncreasethreadImplementsrunnable{7     8     PrivateNumberholder Numberholder;9     Ten      PublicIncreasethread (Numberholder numberholder) { One          This. Numberholder =Numberholder; A     } -  - @Override the      Public voidrun () { -         inti = 0; -          while(i<10){ -             Try { +Thread.Sleep (1000); -}Catch(interruptedexception e) { + e.printstacktrace (); A             } at numberholder.increasenum (); -i++; -         } -     } -}

1  PackageCom.cl.www.thread;2 3 /**4 * Reduce number5  *6  */7  Public classDecreasethreadImplementsrunnable{8     9     PrivateNumberholder Numberholder;Ten      One      PublicDecreasethread (Numberholder numberholder) { A          -          This. Numberholder =Numberholder; -     } the @Override -      Public voidrun () { -         intI =0; -          while(i<10){ +             Try { -Thread.Sleep ((Long) Math.random () *1000); +}Catch(interruptedexception e) { A e.printstacktrace (); at             } - numberholder.decreasenum (); -i++; -         } -     } -}

1  PackageCom.cl.www.thread;2 3  Public classMaintest {4      Public Static voidMain (string[] args) {5         6Numberholder Numberholder =NewNumberholder ();7         8Increasethread Increasethread =NewIncreasethread (numberholder);9Decreasethread Decreasethread =NewDecreasethread (numberholder);Ten          OneThread Th1 =NewThread (Increasethread);//Plus number AThread Th2 =NewThread (Decreasethread);//minus number -          -Thread Th3 =NewThread (Increasethread);//Plus number theThread Th4 =NewThread (Decreasethread);//minus number -          - Th1.start (); - Th2.start (); +          - Th3.start (); + Th4.start (); A     } at}

Output results

Java Multithreaded Communication Example

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.