java线程优先级

java 中的线程优先级的范围是1~10,默认的优先级是5。10最高。

MIN_PRIORITY  1

MAX_PRIORITY 10

NORM_PRIORITY 5

优先级高的获得cpu的几率更大些,不是优先级高的就先执行完,线程优先级随机特性

在java中,线程的优先级具有继承性,例如A线程启动B线程,则A和B的优先级是一样的

线程创建后,可通过调用setPriority()方法改变优先级。

public class Test5 {

    public static class TheadT extends Thread{
        @Override
        public void run() {
            while (true) {
                System.out.println(Thread.currentThread().getName());
            }
        }
    }

    public static void main(String[] args) {
        Thread t1=new TheadT();
        t1.setName("t1");
        Thread t2=new TheadT();
        t2.setName("t2");

        t1.setPriority(Thread.MIN_PRIORITY);
        t2.setPriority(Thread.MAX_PRIORITY);

        t1.start();
        t2.start();
    }
}

  

posted @ 2019-10-09 15:37  MartinEDM  阅读(185)  评论(0编辑  收藏  举报