1 PackageCom.hanqi.xc;2 3 Public classTest1 {4 5 Public Static voidMain (string[] args) {6 7 //Threading Test8 for(inti = 0; I < 10; i++) {9System.out.println ("i =" +i);Ten One //controlling threads through thread classes A Try { - //let the main thread hibernate for 100 milliseconds -Thread.Sleep (100); the -}Catch(interruptedexception e) { - //TODO Auto-generated catch block - e.printstacktrace (); + } - + } A atTest2 t2 =NewTest2 (); - - //Single Thread - //t2.test (); - - //start a new thread in - T2.start (); to +Test2 t3 =NewTest2 (); - the T3.start (); * $Test2 T4 =NewTest2 ();Panax Notoginseng - T4.start (); the + //to start the interface mode of the sub-threading A theThread th =NewThread (NewTest3 (), "Interface thread 4"); + - Th.start (); $ } $ - - the - Wuyi the - Wu -}
PackageCom.hanqi.xc;//support multithreading in an inherited way Public classTest2extendsThread {//overriding the Run method@Override Public voidrun () {//statements that need to be executed concurrentlytest (); } //test Method Public voidTest () { for(inti = 0; I < 10; i++) {System.out.println ("I =" +i); //controlling threads through thread classes Try { //let the main thread hibernate for 100 millisecondsThread.Sleep (100); } Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); } } }
PackageCom.hanqi.xc; Public classTest3ImplementsRunnable {@Override Public voidrun () {//code that needs to be executed in a thread for(inti = 0; I < 10; i++) {System.out.println ("I =" +i); //controlling threads through thread classes Try { //let the main thread hibernate for 100 millisecondsThread.Sleep (100); } Catch(interruptedexception e) {//TODO Auto-generated catch blockE.printstacktrace (); } } }}
Learn to summarize Java threads