美文网首页
Java并发编程:线程的优先级

Java并发编程:线程的优先级

作者: singleZhang2010 | 来源:发表于2020-11-26 16:39 被阅读0次

java中设置线程的优先级使用 setPriority()方法,JDK源码如下:

    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);
        }
    }

线程的优先级有1~10 10个等级,在范围之外则抛出异常:throw new IllegalArgumentException()
JDK中预定义了三个优先级常量的值:

    /**
     * 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;

线程的优先级具有继承性

所谓继承性,就是例如:A线程启动B线程,则B线程具有与A线程相同的优先级。
通过打印this.getPriority()的值可以查看。

线程的优先级具有规则性

优先级越高的越容易被CUP先执行完,和代码的顺序无关

线程的优先级具有随机性

前边说过优先级越高越容易先执行完,而不是完全,只是大概率,因为线程的执行是随机的,设置高的优先级,只是提高先被执行的概率。
不要把优先级与运行结果的顺序作为衡量的标志,优先级高的线程并不一定先执行完run(),线程的优先级与打印顺序无关,它们的关系具有不确定性和随机性
示例:
ThreadPriorityA.java

package com.zhxin.threadLab;

/**
 * @ClassName ThreadPriorityA
 * @Description //countPriority
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2020/11/26 0026 下午 4:24
 **/
public class ThreadPriorityA extends Thread {

    private int count = 0;
    public int getCount(){
        return count;
    }

    @Override
    public void run(){
        while (true){
            count ++;
        }
    }
}

ThreadPriorityB.java

package com.zhxin.threadLab;

/**
 * @ClassName ThreadPriorityB
 * @Description //countPriority
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2020/11/26 0026 下午 4:26
 **/
public class ThreadPriorityB extends Thread {
    private int count = 0;
    public int getCount(){
        return count;
    }

    @Override
    public void run(){
        while (true){
            count ++;
        }
    }
}

CountPriorityTest.java

package com.zhxin.threadLab.test;

import com.zhxin.threadLab.ThreadPriorityA;
import com.zhxin.threadLab.ThreadPriorityB;

/**
 * @ClassName CountPriorityTest
 * @Description //CountPriorityTest
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2020/11/26 0026 下午 4:26
 **/
public class CountPriorityTest {

    public static void main(String[] args){
        try {
            ThreadPriorityA a = new ThreadPriorityA();
            a.setPriority(Thread.MIN_PRIORITY);
            a.start();
            ThreadPriorityB b = new ThreadPriorityB();
            b.setPriority(Thread.MAX_PRIORITY);
            b.start();
            Thread.sleep(1000);
            a.stop();// 不建议用stop,不过测试没关系
            b.stop();
            System.out.println("aCount="+a.getCount());
            System.out.println("bCount="+b.getCount());

        }catch (InterruptedException e){
            e.printStackTrace();
        }
    }
}

运行结果如下:


countPriority

相关文章

网友评论

      本文标题:Java并发编程:线程的优先级

      本文链接:https://www.haomeiwen.com/subject/ytciiktx.html