===============
== ffff65535 ==
===============
十六个一[1111111111111111]

线程优先级

thread
  • Java提供一个线程调度器来监控程序中启动后进入就绪状态的所有线程,线程调度器按照优先级决定应该调度哪个线程来执行
  • 线程优先级用数字表示,范围从1~10(线程的调度由CPU决定,数字越大概率越大)
    • Thread.MIN_PRIORITY = 1
    • Thread.NORM_PRIORITY = 5
    • Thread.MAX_PRIORITY = 10
  • 使用以下方法改变或获取线程优先级
    • getPriority()
    • setPriority(int xxx)
public class TestThreadPriority {
    public static void main(String[] args) {
        // 打印主线程默认优先级
        System.out.println(Thread.currentThread().getName() + "====" + Thread.currentThread().getPriority());

        Priority priority = new Priority();

        Thread t1 = new Thread(priority, "t1");
        Thread t2 = new Thread(priority, "t2");
        Thread t3 = new Thread(priority, "t3");
        Thread t4 = new Thread(priority, "t4");
        Thread t5 = new Thread(priority, "t5");
        Thread t6 = new Thread(priority, "t6");

        t1.start();
        t2.setPriority(1);
        t2.start();
        t3.setPriority(4);
        t3.start();
        t4.setPriority(Thread.MAX_PRIORITY);
        t4.start();
        t5.setPriority(6);
        t5.start();
        t6.setPriority(8);
        t6.start();
    }
}

class Priority implements Runnable {
    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + "====" + Thread.currentThread().getPriority());
    }
}

参考资料:

https://www.bilibili.com/video/BV1V4411p7EF?p=16