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