1.join
PackageCom.thread.control; Public classControlbyjoinextendsthread{ PublicControlbyjoin (String name) {Super(name); } Public voidrun () { for(inti=0;i<100;i++) {System.out.println ("Current Thread Name:" + Thread.CurrentThread (). GetName () + "+" Num: "+i); } } Public Static voidMain (string[] args) {//TODO auto-generated Method Stub for(inti=0;i<100;i++){ if(i==20) {Controlbyjoin CJ=NewControlbyjoin ("The thread is joined"); Cj.start (); Try{ cj.join (); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); }} System.out.println ("Current Thread Name:" + Thread.CurrentThread (). GetName () + "+" Num: "+i); } }}
Run results
... current Thread name:main Num:17Current Thread name:main Num:18Current Thread name:main Num:19Current thread name:the thread is joined Num:0Current thread name:the thread is joined Num:1Current thread name:the thread is joined Num:2Current thread name:the thread is joined Num:3... current thread name:the thread is joined Num:96Current thread name:the thread is joined Num:97Current thread name:the thread is joined Num:98Current thread name:the thread is joined Num:99Current Thread name:main Num:20Current Thread name:main Num:21stCurrent Thread name:main Num:22... current Thread name:main Num:95Current Thread name:main Num:96Current Thread name:main Num:97Current Thread name:main Num:98Current Thread name:main Num:99
2. Setdaemon
PackageCom.thread.control; Public classControlbysetdaemonextendsthread{ PublicControlbysetdaemon (String name) {Super(name); } Public voidrun () { for(inti=0;i<1000;i++) {System.out.println (GetName ()+" "+i); } } Public Static voidMain (string[] args) {Controlbysetdaemon sd=NewControlbysetdaemon ("Daemon Thread"); Sd.setdaemon ( true); Sd.start (); for(inti=0;i<100;i++) {System.out.println (Thread.CurrentThread (). GetName ()+" "+i); } }}
As you can see from the running results, Daemon thread did not run to natural death, but was killed at the end of the foreground Mian thread.
3.sleep
PackageCom.thread.control; Public classControlbysleepextendsthread{ Publiccontrolbysleep (String name) {Super(name); } Public voidrun () { for(inti=0; i<100; i++) {System.out.println (GetName ()+ " " +i); } } Public Static voidMain (string[] args) {controlbysleep s=NewControlbysleep ("New Thread")); S.start (); for(inti=0; i<100; i++) {System.out.println (Thread.CurrentThread (). GetName ()+ " " +i); //when I is 20 o'clock, the main thread sleep3 seconds if(i==20) {System.out.println ("Begin Sleep"); Try{ Thread.Sleep ( the); } Catch(interruptedexception e) {//TODO auto-generated Catch blockE.printstacktrace (); } System.out.println ("End Sleep"); } } }}
Run results
From the results you can see that the new thread has finished running in the 3 second of the main thread sleep.
. .. main17NewThread 17Main18NewThread 18Main19NewThread 19Main20NewThread 20Begin sleepNewThread 21NewThread 22NewThread 23NewThread 24....NewThread 95NewThread 96NewThread 97NewThread 98NewThread 99End Sleepmain21stMain22Main23Main24Main25....
4.yield
PackageCom.thread.control; Public classControlbyyieldextendsthread{ PublicControlbyyield (String name) {Super(name); } Public voidrun () { for(inti=0;i<100;i++) {System.out.println (GetName ()+ " " +i); if(i==20) { Thread.yield (); } } } Public Static voidMain (string[] args) {Controlbyyield y1=NewControlbyyield ("Thread 1"); Y1.setpriority (max_priority); Controlbyyield y2=NewControlbyyield ("Thread 2"); Y2.setpriority (min_priority); Y1.start (); Y2.start (); }}
Run results
From the running results, you can see that the thread 1 is executed first.
11, 11 981,2 12 22 32 42 5 ...
5. SetPriority
PackageCom.thread.control; Public classControlbypriorityextendsthread{ Publiccontrolbypriority (String name) {Super(name); } Public voidrun () { for(inti = 0; i<100;i++) {System.out.println (GetName ()+ "+" The priority is: "+getpriority ()+ "+" The value of I is: "+i); } } Public Static voidMain (string[] args) {thread.currentthread (). SetPriority (6); System.out.println (Thread.CurrentThread (). GetName ()+ "+" the priority of Main thread is: "+Thread.CurrentThread (). getpriority ()); for(inti=0;i<100;i++){ if(i==10) {controlbypriority low=NewControlbypriority ("Low"); Low.start (); System.out.println ("The priority of a thread low is:" +low.getpriority ()); low.setpriority (min_priority); } if(i==20) {controlbypriority high=NewControlbypriority ("High"); High.start (); System.out.println ("The priority of thread high is:" +high.getpriority ()); high.setpriority (max_priority); } } }}
Line Program Control system