package com.Java;

//线程优先级调度
// 注意:不是调整了优先级就一定会被cpu先执行 只是提高了执行概率 一切还是要看cpu调度
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);
Thread t2 = new Thread(myPriority);
Thread t3 = new Thread(myPriority);
Thread t4 = new Thread(myPriority);
Thread t5 = new Thread(myPriority);
Thread t6 = new Thread(myPriority);

//先设置优先级再启动 main默认是优先的
t1.setPriority(Thread.MAX_PRIORITY);
t1.start();

t2.setPriority(9);
t2.start();

t3.setPriority(5);
t3.start();

t4.setPriority(1);
t4.start();

t5.setPriority(6);
t5.start();

t6.setPriority(3);
t6.start();
}
}

class MyPriority implements Runnable {

@Override
public void run() {
System.out.println(Thread.currentThread().getName() + "-->" + Thread.currentThread().getPriority());//getPriority 获取线程的优先级常量
}
}