Java multithreaded open three threads output ABC10 times

Source: Internet
Author: User

Recently learned multi-threading, search, full screen problems are similar to the title of the same, so take this as a start, I tried a hand,

Several times the computer CPU ran to 100%, finally still write out, the general idea is:

Declares a variable that marks three threads for which thread to output, each output will be the variable +1, judging by the variable to 3 of the remainder, if 1-a,2-b, 3-c

1  Public classABC {2 3     Private Static intMark = 0;4 5     Private StaticObject obj =NewObject ();6     7      Public Static voidMain (string[] args)throwsException {8ABC abc =NewABC ();9         NewThread (ABC.NewPrintA ()). Start ();Ten         NewThread (ABC.NewPRINTB ()). Start (); One         NewThread (ABC.NewPRINTC ()). Start (); A     } -      -     classPrintAImplementsrunnable{ the  - @Override -          Public voidrun () { -              for(inti = 0; I < 10; i++){ +                 synchronized(obj) { -                      while(Mark%3! = 0){ +                         Try { A obj.wait (); at}Catch(interruptedexception e) { - e.printstacktrace (); -                         } -                     } -System.out.print ("A"); -Mark + +; in Obj.notifyall (); -                 } to             } +         } -     } the     classPrintbImplementsrunnable{ *  $ @OverridePanax Notoginseng          Public voidrun () { -              for(inti = 0; I < 10; i++){ the                 synchronized(obj) { +                      while(Mark%3! = 1){ A                         Try { the obj.wait (); +}Catch(interruptedexception e) { - e.printstacktrace (); $                         } $                     } -System.out.print ("B"); -Mark + +; the Obj.notifyall (); -                 }Wuyi             } the         } -     } Wu     classPrintcImplementsrunnable{ -  About @Override $          Public voidrun () { -              for(inti = 0; I < 10; i++){ -                 synchronized(obj) { -                      while(Mark%3! = 2){ A                         Try { + obj.wait (); the}Catch(interruptedexception e) { - e.printstacktrace (); $                         } the                     } theSystem.out.println ("C"); theMark + +; the Obj.notifyall (); -                 } in             } the         } the     } About}

Java multithreaded open three threads output ABC10 times

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.