線程優(yōu)先級(jí)分為10個(gè)等級(jí)糠悯,主線程優(yōu)先級(jí)不變,優(yōu)先級(jí)越高代表執(zhí)行的順序越靠前牢裳,但不排除出現(xiàn)優(yōu)先級(jí)低的線程先運(yùn)行逢防。
線程若想設(shè)置優(yōu)先級(jí),必須先設(shè)置再啟動(dòng)F蜒丁M!
package com.example.demo.thread;
/**
* @projectName: demo
* @package: com.example.demo.thread
* @className: TestPriority
* @author:
* @description: 測試線程的優(yōu)先級(jí)
* @date: 2021/12/7 22:06
*/
public class TestPriority {
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+":------------------>"+Thread.currentThread().getPriority());
MyPriority myPriority = new MyPriority();
Thread t1 = new Thread(myPriority,"t1");
Thread t2 = new Thread(myPriority,"t2");
Thread t3 = new Thread(myPriority,"t3");
Thread t4 = new Thread(myPriority,"t4");
Thread t5 = new Thread(myPriority,"t5");
t1.setPriority(Thread.MIN_PRIORITY);
t2.setPriority(Thread.MAX_PRIORITY);
t3.setPriority(Thread.NORM_PRIORITY);
t4.setPriority(7);
t5.setPriority(3);
t1.start();
t2.start();
t3.start();
t4.start();
t5.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+":------------------>"+Thread.currentThread().getPriority());
}
}