Java Multi-threaded communication two threads individually print AB 10 times

Source: Internet
Author: User

A classic interview topic: Two threads, respectively, print AB, where thread a prints a, thread B prints B, each printing 10 times to make it appear ababababa. The effect

1  PackageCom.shangshe.path;2 3  Public classThreadab {4 5     /**6      * @paramargs7      */8      Public Static voidMain (string[] args) {9         Ten         FinalPrint business =NewPrint (); One          A         NewThread (NewRunnable () { -              Public voidrun () { -                  for(inti=0;i<10;i++) { the business.print_a (); -                 } -             } - }). Start (); +          -         NewThread (NewRunnable () { +              Public voidrun () { A                  for(inti=0;i<10;i++) { at business.print_b (); -                 } -             } - }). Start (); -          -     } in } - classPrint { to      +     Private BooleanFlag =true; -      the      Public synchronized voidprint_a () { *          while(!flag) { $             Try {Panax Notoginseng                  This. Wait (); -}Catch(interruptedexception e) { the                 //TODO auto-generated Catch block + e.printstacktrace (); A             } the         } +System.out.print ("A"); -Flag =false; $          This. Notify (); $     } -      -      Public synchronized voidPrint_b () { the          while(flag) { -             Try {Wuyi                  This. Wait (); the}Catch(interruptedexception e) { -                 //TODO auto-generated Catch block Wu e.printstacktrace (); -             } About         } $System.out.print ("B"); -Flag =true; -          This. Notify (); -     } A}
View Code

From the above example we can design 3 threads and N Threads of the program, the following example is 3 threads, respectively print a,b,c 10 times, so that it appears abcabc. The effect

1  Public classTHREADABC {2 3     /**4      * @paramargs5      */6      Public Static voidMain (string[] args) {7         8         FinalPrint business =NewPrint ();9         Ten         NewThread (NewRunnable () { One              Public voidrun () { A                  for(inti=0;i<100;i++) { - business.print_a (); -                 } the             } - }). Start (); -          -         NewThread (NewRunnable () { +              Public voidrun () { -                  for(inti=0;i<100;i++) { + business.print_b (); A                 } at             } - }). Start (); -          -         NewThread (NewRunnable () { -              Public voidrun () { -                  for(inti=0;i<100;i++) { in Business.print_c (); -                 } to             } + }). Start (); -          the     } * } $ classPrint {Panax Notoginseng      -     Private BooleanShould_a =true; the     Private BooleanShould_b =false; +     Private BooleanShould_c =false; A      the      Public synchronized voidprint_a () { +          while(Should_b | |Should_c) { -             Try { $                  This. Wait (); $}Catch(interruptedexception e) { -                 //TODO auto-generated Catch block - e.printstacktrace (); the             } -         }WuyiSystem.out.print ("A"); theShould_a =false; -Should_b =true; WuShould_c =false; -          This. Notifyall (); About     } $      -      Public synchronized voidPrint_b () { -          while(Should_a | |Should_c) { -             Try { A                  This. Wait (); +}Catch(interruptedexception e) { the                 //TODO auto-generated Catch block - e.printstacktrace (); $             } the         } theSystem.out.print ("B"); theShould_a =false; theShould_b =false; -Should_c =true; in          This. Notifyall (); the     } the      About      Public synchronized voidPrint_c () { the          while(Should_a | |should_b) { the             Try { the                  This. Wait (); +}Catch(interruptedexception e) { -                 //TODO auto-generated Catch block the e.printstacktrace ();Bayi             } the         } theSystem.out.print ("C"); -Should_a =true; -Should_b =false; theShould_c =false; the          This. Notifyall (); the     } the}
View Code

Once again proves the importance of software engineering; In a multithreaded program, it should be said that in the program, we should put those business logic code in the same class, so that the high cohesion, low coupling

Java Multi-threaded communication two threads individually print AB 10 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.