Thread Priority
/ * thread priority, range [1,10] * Different priority threads get different opportunities for execution, the higher the priority, the greater the execution opportunity * * Compared to the chance that different priority threads are executed. Public class Test05 { Public Static voidMain (string[] args) {System.out.println ("Maximum priority:"+ thread.max_priority); System.out.println ("Minimum priority:"+ thread.min_priority); System.out.println ("Medium Priority:"+ thread.norm_priority); System.out.println ("PRIMARY thread Default priority:"+ Thread.CurrentThread (). getpriority ()); System.out.println ("***********************\n"); MyThread3 t1=NewMyThread3 (); T1.setpriority (Ten); T1.start (); MyThread4 t2=NewMYTHREAD4 (3); T2.start (); while(true) {System.out.println (Thread.CurrentThread (). GetName () +", Priority:"+ Thread.CurrentThread (). getpriority ()); } }} class MyThread3 extends Thread {@Override Public voidRun () { while(true) {System.out.println (Thread.CurrentThread (). GetName () +", Priority:"+ getpriority ()); } }} class MyThread4 extends Thread { PublicMYTHREAD4 (intPriority) {setpriority (priority); } @Override Public voidRun () { while(true) {System.out.println (Thread.CurrentThread (). GetName () +", Priority:"+ getpriority ()); } }}
Sleep () and yield ()
Import Java.text.simpledateformat;import java.util.Date;/ * * sleep () and yield () */ Public class Test06 { Public Static voidMain (string[] args) {/*MYTHREAD5 mt = new MyThread5 (250); Thread thread = new Thread (MT, "first"); Thread.Start (); */MYTHREAD6 MT =NewMyThread6 (); Thread thread =NewThread (MT,"Second"); Thread.Start (); while(true) {Thread.yield (); System.out.println (Thread.CurrentThread (). GetName () +"****"); } }} class MyThread5 implements Runnable { Private intNum PublicMYTHREAD5 (intNUM) { This. num = num; } @Override Public voidRun () { while(Num >0) {Try{Thread.Sleep (8000);//sleep The thread from the execution state into hibernationSystem.out.println (Thread.CurrentThread (). GetName () +"****"+ num--); }Catch(Interruptedexception e) {E.printstacktrace (); } } }} class MyThread6 implementsRunnable{ @Override Public voidRun () {SimpleDateFormat sdf=NewSimpleDateFormat ("Yyyy-mm-dd hh:mm:ss"); while(true){//yield method to get threads from execution state into ready stateThread.yield (); System.out.println (Thread.CurrentThread (). GetName () +", Current time:"+sdf.format (NewDate ())); } }}
Join () method
/* * JOIN () method */ Public class Test07 { Public Static voidMain (string[] args) {MyThread7 MT =NewMyThread7 (); Thread T1 =NewThread (MT,"First"); Thread t2 =NewThread (MT,"Second"); T1.start (); T2.start ();//When thread two executes and then performs the main thread Try{System.out.println ("T2 is alive?"+t2.isalive ()); T2.join (); System.out.println ("T2 is alive?"+t2.isalive ()); }Catch(Interruptedexception e) {E.printstacktrace (); } for(inti =1; I <= -; i++) {System.out.println (Thread.CurrentThread (). GetName () +"****"+ i); } }} class MyThread7 implements Runnable {@Override Public voidRun () { for(inti =1; I <= -; i++) {System.out.println (Thread.CurrentThread (). GetName () +"****"+ i); } }}
Java Learning Note (39)-Thread priority