Introduction to Java Thread Priority:
1. Java provides a thread scheduler to monitor all threads in the program that have entered a ready state after being started.
Determines which thread should be dispatched to execute according to the priority of the thread.
2. The priority of a thread is represented by a number, ranging from 1 to 10, and the default priority for a thread is 5.
thread.min_priority = 1;
Thread.max_priority = 10;
thread.norm_priority = 5;
3. Use the following method to get or set the priority of the thread object.
int getpriority ();
void setpriority (int newpriority);
"Note" The higher the priority of the thread, the more time slices will be executed by the CPU. (Each thread has its own priority)
Demo_1:
public class Testpriority {public static void main (string[] args) {T1 T1 = new T1 (); T2 t2 = new T2 (); Thread tt1 = new Thread (t1); Thread tt2 = new Thread (t2); Tt1.setpriority (thread.norm_priority + 3); The priority of the TT1 is increased, the time slice that gets CPU executes is tt1.start (); Tt2.start ();}} Class T1 implements Runnable {@Overridepublic void Run () {for (int i = 0; i <; i++) {System.out.println ("T1:" +i);} }class T2 implements Runnable {@Overridepublic void Run () {for (int i = 0; i <; i++) {System.out.println ("---T2:" +i) ;}}}
Demo_2:
The priority of threads in Java