Java Multi-Threading and concurrent libraries advanced applications-traditional thread synchronous communication technology

Source: Internet
Author: User

Interview questions:

The child thread loops 10 times, then the main thread loops 100 times, then goes back to the child thread loop 10 times, then the main thread loop 100 times, so loop 50 times, please write the program

/*** Sub-thread loop 10 times, then the main thread loop 100 times, then back to the child thread loop 10 times, and then the main thread loop 100 times, so loop 50 times, please write the program * *@authorAdministrator **/ Public classtraditionalthreadcommunication { Public Static voidMain (string[] args) {FinalBusiness Business =NewBusiness (); NewThread (NewRunnable () {@Override Public voidrun () { for(inti = 1; I <= 50; i++) {business.sub (i);        }}). Start ();  for(inti = 1; I <= 50; i++) {business.main (i); }    }}classBusiness {Private BooleanIsshoudesub =true; /*** Synchronize with the resources you need to access the code, do not put on the thread *@paramI*/     Public synchronized voidSubinti) { while(!isshoudesub) {//It is also possible to use the IF, while to compare some as in the one argument version, interrupts and spurious wakeups is possible, and this method Should always being used in a loop            Try{//prevents threads from being able to be woken up (while placed here to raise the level)                 This. Wait (); } Catch(interruptedexception e) {e.printstacktrace (); }        }         for(intj = 1; J <= 100; J + +) {System.out. println ("Sub thread sequence of" + j + ", loop of" +i); } isshoudesub=false;  This. Notify (); }     Public synchronized voidMaininti) { while(isshoudesub) {Try {                 This. Wait (); } Catch(interruptedexception e) {e.printstacktrace (); }        }         for(intj = 1; J <= 10; J + +) {System.out.println ("Main thread sequence of" + j + ", loop of" +i); } isshoudesub=true;  This. Notify (); }    /*** * Synchronized (obj) {Here obj must be the same as obj.wait, otherwise it throws an exception while (<condition does not hold>)         Obj.wait (); ...//Perform action appropriate to condition}*/}

Several methods to use common data (including synchronous locks) or common algorithms should be grouped in the same class, which is a design that introduces high clustering and robustness of programs.

Java Multi-Threading and concurrent libraries advanced applications-traditional thread synchronous communication technology

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.