线程的优先级
-
java提供了一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定调度那个线程优先执行
-
线程的优先级用数字表示,范围从1~10
-
Thread.MIN_PRIONRITY = 1;
-
Thread.MAX_PRIORITY = 10;
-
Thread.NORMJ_PRIORITY = 5;
-
可以使用以下方式改变或获取优先级
-
getPriority().srtPriority(int xxx)
package BufferedTest;
public class TestPriority extends Thread{
public static void main(String[] args) {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());//主线程默认优先级
MyPriority p1 = new MyPriority();
Thread t1= new Thread(p1);
Thread t2= new Thread(p1);
Thread t3 = new Thread(p1);
Thread t4= new Thread(p1);
Thread t5= new Thread(p1);
t5.setPriority(2);
t5.start();
t4.setPriority(Thread.MAX_PRIORITY);//线程默认最高优先执行
t4.start();
t1.setPriority(9);
t1.start();
}
}
class MyPriority implements Runnable{
@Override
public void run() {
System.out.println(Thread.currentThread().getName()+"-->"+Thread.currentThread().getPriority());//当前线程的名字+优先级
}
}

浙公网安备 33010602011771号