JAVA thread scheduling priority, java Thread Scheduling
The Thread priority is represented by numbers. The default range is 1 to 10, that is, Thread. MIN_PRIORITY to Thread. MAX_PRIORTY. The default priority of a Thread is 5, that is, Thread. NORM_PRIORTY.
Method for priority operation:
Int getPriority (): gets the thread priority.
Void setPriority (int newPriority): After a thread is created, you can use this method to change the thread priority.
It must be noted that the thread priority cannot guarantee the thread execution order. However, threads with a higher priority have a higher probability of obtaining CPU resources.
Class MyThread extends Thread {String message; MyThread (String message) {this. message = message;} public void run () {for (int I = 0; I <3; I ++) {System. out. println (message + "" + getPriority () ;}} public class PriorityThread {public static void main (String args []) {Thread t1 = new MyThread ("T1"); t1.setPriority (Thread. MIN_PRIORITY); t1.start (); Thread t2 = new MyThread ("T2"); t2.setPriority (Thread. MAX_PRIORITY); t2.start (); Thread t3 = new MyThread ("T3"); t3.setPriority (Thread. MAX_PRIORITY); t3.start ();}}