Java 线程优先级

上源码:

public static final int MIN_PRIORITY = 1;
public static final int NORM_PRIORITY = 5;
public static final int MAX_PRIORITY = 10;

private void init(ThreadGroup g, Runnable target, String name,
                      long stackSize, AccessControlContext acc,
                      boolean inheritThreadLocals){
...
this.priority = parent.getPriority();
...
setPriority(priority);    
...
}

public final void setPriority(int newPriority) {
        ThreadGroup g;
        checkAccess();
        if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
            throw new IllegalArgumentException();
        }
        if((g = getThreadGroup()) != null) {
            if (newPriority > g.getMaxPriority()) {
                newPriority = g.getMaxPriority();
            }
            setPriority0(priority = newPriority);
        }
    }

子线程继承父线程的优先级,如果子线程在线程组中,而且父线程的优先级大于线程组的最大优先级,则子线程优先级为线程组的最大优先级。

     main线程优先级默认5,低优先级为1至4,高优先级为6-10,Thread.MIN_PRIORITY为1,Thread.NORM_PRIORITY为5,Thread.MAX_PRIORITY为10。
    高优先级线程不一定比低优先级线程优先执行。受CPU多核和操作系统调度策略影响。
    操作系统中,进程是资源分配的最小单位,线程是最小调度单位。在windows中进程是不活动的,只是作为线程的容器。一个CPU核中低优先级线程可能比另一个CPU核中的高优先级线程执行快。
    Windows优先级推进器中,低优先级线程已经频繁执行,来了高优先级线程,低优先级线程可以越级优先执行。

看实例:

/**
 * @Author: ZhangHao
 * @Description: 线程优先级测试
 * @Date: 2020/7/23 10:56
 * @Version: 1.0
 */
public class ThreadPriorityTest {

    public static void main(String[] args) {
        System.out.println("mian线程优先级:" + Thread.currentThread().getPriority());

        Thread thread = new Thread();
        System.out.println("子线程默认优先级:" + thread.getPriority());

        Thread.currentThread().setPriority(7);
        Thread thread111 = new Thread();
        System.out.println("子线程默认优先级:" + thread111.getPriority());

        ThreadGroup threadGroup1 = new ThreadGroup("threadGroup1");
        System.out.println("线程组最大优先级:" + threadGroup1.getMaxPriority());
        threadGroup1.setMaxPriority(6);

        Thread thread1 = new Thread(threadGroup1, "thread1");
        System.out.println("子线程默认优先级:" + thread1.getPriority());
        thread1.setPriority(10);
        System.out.println("子线程优先级:" + thread1.getPriority());

        Thread thread11 = new Thread(() -> {
            for(int i =0 ;i < 100;i++){
                System.out.println(Thread.currentThread().getName() + " " + i);
            }
        });
        thread11.setPriority(Thread.MAX_PRIORITY);

        Thread thread22 = new Thread(() -> {
            for(int i =0 ;i < 10;i++){
                System.out.println(Thread.currentThread().getName() + " "+ i);
            }
        });
        thread22.setPriority(Thread.MAX_PRIORITY);

        Thread thread33 = new Thread(() -> {
            for(int i =0 ;i < 10;i++){
                System.out.println(Thread.currentThread().getName() + " "+ i);
            }
        });
        thread33.setPriority(Thread.MAX_PRIORITY);

        thread11.start();
        thread22.start();
        thread33.start();
    }

}

结果:

mian线程优先级:5
子线程默认优先级:5
子线程默认优先级:7
线程组最大优先级:10
子线程默认优先级:6
子线程优先级:6
Thread-2 0
Thread-3 0
Thread-3 1
Thread-2 1
Thread-3 2
Thread-4 0
Thread-2 2
Thread-4 1
Thread-4 2
Thread-3 3
Thread-4 3
Thread-2 3
Thread-4 4
Thread-3 4
Thread-4 5
Thread-2 4
Thread-4 6
Thread-3 5
Thread-4 7
Thread-2 5
Thread-4 8
Thread-3 6
Thread-4 9
Thread-2 6
Thread-2 7
Thread-2 8
Thread-2 9
Thread-3 7
Thread-3 8
Thread-3 9

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃峰顶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值