Packagecom.jckb;/**Two methods of multithreading implementation * *@authorGX **/ Public classTest2 { Public Static voidMain (string[] args) {Mythread m=NewMythread (); M.start ();//cannot call the Run method directly//M.run ();//is a method call, not a thread's startThread T =NewThread (NewMythread2 ()); T.start (); }}//the method that the thread stops is the Run method returns//Flag=false Exit//cannot start again after a thread has stoppedclassMythreadextendsThread {@Override Public voidrun () {BooleanFlag =true; inti = 1; while(flag) {System.out.println ("I=" +i); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } I++; if(i = = 5) {flag=false; //return; } } }}classMythread2ImplementsRunnable {@Override Public voidrun () { for(inti = 0; I < 10; i++) {System.out.println ("I=" +i); Try{Thread.Sleep (1000); } Catch(interruptedexception e) {e.printstacktrace (); } } }}
Java Multi-Threading Implementation method