JAVA多线程之优先级设置

JAVA多线程之设置线程的优先级

线程特性

在操作系统中,线程可以划分优先级,优先级较高的线程得到的CPU资源要多一些,也就是CPU优先执行优先级较高的线程对象中的任务,其实就是让高优先级的线程获得更多的CPU时间片,但只是高优先级的线程获取资源的概率会大一点,并不一定保证一定就最先执行,这个取决于CPU

setPriority()方法

此方法在JDK中的源码如下

/**
     * Changes the priority of this thread.
     * <p>
     * First the <code>checkAccess</code> method of this thread is called
     * with no arguments. This may result in throwing a
     * <code>SecurityException</code>.
     * <p>
     * Otherwise, the priority of this thread is set to the smaller of
     * the specified <code>newPriority</code> and the maximum permitted
     * priority of the thread's thread group.
     *
     * @param newPriority priority to set this thread to
     * @exception  IllegalArgumentException  If the priority is not in the
     *               range <code>MIN_PRIORITY</code> to
     *               <code>MAX_PRIORITY</code>.
     * @exception  SecurityException  if the current thread cannot modify
     *               this thread.
     * @see        #getPriority
     * @see        #checkAccess()
     * @see        #getThreadGroup()
     * @see        #MAX_PRIORITY
     * @see        #MIN_PRIORITY
     * @see        ThreadGroup#getMaxPriority()
     */
    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);
        }
    }

在这里插入图片描述
可以看到注释:更改线程的优先级有预定义几个优先级MIN_PRIORITY -到MAX_PRIORITY之间
也就是优先级分为1-10个等级,如果不在这个范围内则抛出异常IllegalArgumentException
一下是默认设置的几个级别:1、5、10 1最低,10最高,如果不设置,默认是5

/**
     * The minimum priority that a thread can have.
     */
    public final static int MIN_PRIORITY = 1;

   /**
     * The default priority that is assigned to a thread.
     */
    public final static int NORM_PRIORITY = 5;

    /**
     * The maximum priority that a thread can have.
     */
    public final static int MAX_PRIORITY = 10;

但线程并不一定会按照这个优先级区执行,只是设置了优先级高的线程获取CUP资源的概率会大一点,但并不能保证百分百就是高优先级的线程最先执行
例如

 public static void main(String[] arg){
        MyThread myThread = new MyThread("A");
        myThread.setPriority(1);
        MyThread myThread2 = new MyThread("B");
        myThread2.setPriority(10);
        MyThread myThread3 = new MyThread("C");
        myThread.start();
        myThread2.start();
        myThread3.start();
    }



    static class MyThread extends Thread{
        public MyThread(String name){
            super(name);
        }

        @Override
        public void run() {
            for (int i = 0 ;i<10; i++){
                System.out.println(i+"<------------->"+Thread.currentThread().getName());
            }
        }
    }

结果

0<------------->B
0<------------->C
1<------------->C
2<------------->C
3<------------->C
0<------------->A
4<------------->C
5<------------->C
6<------------->C
7<------------->C
8<------------->C
9<------------->C
1<------------->B
2<------------->B
3<------------->B
4<------------->B
5<------------->B
1<------------->A
6<------------->B
7<------------->B
8<------------->B
9<------------->B
2<------------->A
3<------------->A
4<------------->A
5<------------->A
6<------------->A
7<------------->A
8<------------->A
9<------------->A

可以从结果看到A,B,C是交互执行,但B和C执行的顺序会在A前面(这个并不是百分百一点的),但这个执行结果C的执行顺序要高于B,再次执行

0<------------->B
1<------------->B
2<------------->B
3<------------->B
4<------------->B
5<------------->B
6<------------->B
7<------------->B
8<------------->B
9<------------->B
0<------------->C
1<------------->C
2<------------->C
3<------------->C
4<------------->C
5<------------->C
6<------------->C
7<------------->C
8<------------->C
9<------------->C
0<------------->A
1<------------->A
2<------------->A
3<------------->A
4<------------->A
5<------------->A
6<------------->A
7<------------->A
8<------------->A
9<------------->A

可以看到这次的结果和上次有不同
总结:线程启动后纳入到线程调度,线程时刻处于被动获取CPU时间片而无法主动获取。我们可以通过调整线程的优先级来最大程度的干涉线程调度分配时间片的几率,但是能不能抢到资源也是不一定,这个取决于CPU。

大家有问题评论区见!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值