(Dark Horse Java multi-threading and concurrent Library advanced application) 04 traditional thread synchronous communication technology

Source: Internet
Author: User
Tags stub

The child thread is 10 times, then the main thread 100 times, then the child thread 10 times, and then the main thread 100 times. Cycle 50 times

 PackageCn.itcast.heima2; Public classtraditionalthreadcomunication { Public Static voidMain (string[] args) {//TODO auto-generated Method StubBusiness Business =NewTraditionalthreadcomunication ().NewBusiness (); NewThread (NewRunnable () {@Override Public voidrun () {//TODO auto-generated Method Stub                 for(inti = 0; I < 50; i++) {business.sub (i);                }}). Start ();  for(inti = 0; I < 50; i++) {business.main (i); }    }    classBusiness {Private Booleanbshouldsub=true;  Public synchronized voidSubinti) {if(!bshouldsub) {                Try {                     This. Wait (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }             for(intj = 0; J < 10; J + +) {System.out.println ("Sub thread Sequence:" + j + "loop of" +i); } bshouldsub=false;  This. Notify (); }         Public synchronized voidMaininti) {if(bshouldsub) {Try {                     This. Wait (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }            }             for(intj = 0; J < 100; J + +) {System.out.println ("Main thread sequence:" + j + "loop of" +i); } bshouldsub=true;  This. Notify (); }            }}

(Dark Horse Java multi-threading and concurrent Library advanced application) 04 traditional thread synchronous communication technology

Related Article

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.