当前位置:网站首页>线程的调度、线程的优先级

线程的调度、线程的优先级

2022-06-09 06:55:00 小码哥呀

1、线程的调度

在这里插入图片描述

2、线程的优先级

在这里插入图片描述

/** * 1、线程的优先级 * MAX_PRIORITY:10 * MIN_PRIORITY:1 * NORM_PRIORITY:5 * 2、如何获取和设置当前线程的优先级 * getPriority():获取线程的优先级 * setPriority():设置线程的优先级 * * 说明:高优先级的现线程要抢占低优先级cpu的执行权,但是只是从概率上讲,高优先级的线程高概率的情况下被执行。 * 并以意味着只有当高优先级的线程执行完以后,低优先级的线程才执行。 * * DATE: 2022/6/5 22:35 */
class HelloThread2 extends Thread{
    
    @Override
    public void run() {
    
        for(int i = 0;i < 100;i++) {
    
            if (i % 2 == 0) {
    
                System.out.println(Thread.currentThread().getName() + ":"+Thread.currentThread().getPriority()+":"+ i);
            }
        }
    }
}
public class ThreadPriority {
    
    public static void main(String[] args) {
    
        HelloThread2 h2 = new HelloThread2();
        //给当前线程设置名字
        h2.setName("线程h2");
        //设置分线程的优先级
        h2.setPriority(Thread.MAX_PRIORITY);
        h2.start();

        Thread.currentThread().setName("主线程");
        //设置主线程的优先级
        Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
        for(int i = 0;i < 100;i++) {
    
            if (i % 2 == 0) {
    
                System.out.println(Thread.currentThread().getName() + ":" +Thread.currentThread().getPriority()+":"+ i);
            }
        }
        
    }
}

原网站

版权声明
本文为[小码哥呀]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_46112274/article/details/125138227