# 优先级

  • 线程优先级会提示(hint)调度器优先调度该线程,但它仅仅是一个提示,调度器可以忽略它
  • 如果 cpu 比较忙,那么优先级高的线程会获得更多的时间片,但 cpu 闲时,优先级几乎没作用
@Slf4j
public class Demo {
    public static void main(String[] args) {

        Thread thread = new Thread(() -> {
            log.info("{}", Thread.currentThread().getName() + "run..");
        }, "custom-thread");

        Thread thread2 = new Thread(() -> {
            log.info("{}", Thread.currentThread().getName() + "run..");
        }, "custom-thread2");

        //设置优先级
        thread.setPriority(Thread.NORM_PRIORITY);
        thread2.setPriority(Thread.MIN_PRIORITY);

        thread.start();
        thread2.start();

    }
}