线程的优先级

在操作系统中,线程可以划分优先级的,优先级较高的线程得到的CPU资源较多。(也就是说比那些优先级比自己低的线程执行速度快)

设置线程的优先级使用setPriority()方法。

在java中,线程的优先级可以分为1-10个等级,小于1或者大于10都会抛出java.lang.IllegalArgumentException异常,默认优先级为5。

优先级的特性:(先列举出来,下面将一一讲解)

  1、优先级的继承性。

  2、优先级的规则性。

  3、优先级的随机性。

1、线程优先级的继承性:

  优先级是有机场性的,比如A线程启动B线程,则B线程的优先级和A线程的优先级是一样的。

下面看一下设置优先级的例子:

public class Test01 {
    //优先级的继承性
    public static void main(String[] args) {
        test01 test01 = new test01();
        test01.start();
    }

    public static class test01 extends Thread {
        @Override
        public void run() {
            super.run();
            System.out.println("test01线程悠闲级为:" + this.getPriority());
            //test02继承的test01
            test02 test02 = new test02();
            test02.start();
        }
    }

    public static class test02 extends Thread {
        @Override
        public void run() {
            super.run();
            System.out.println("test02线程悠闲级为:" + this.getPriority());
        }
    }

}

//打印语句

test01线程悠闲级为:5
test02线程悠闲级为:5

Process finished with exit code 0

使用setPriority()设置线程优先级:

public class Test01 {
    
    public static void main(String[] args) {
        test01 test01 = new test01();
        test01.start();
    }

    public static class test01 extends Thread {
        @Override
        public void run() {
            super.run();
            System.out.println("test01线程悠闲级为:" + this.getPriority());
            test02 test02 = new test02();
            //自定义test02优先级
            test02.setPriority(8);
            test02.start();
        }
    }

    public static class test02 extends Thread {
        @Override
        public void run() {
            super.run();
            System.out.println("test02线程悠闲级为:" + this.getPriority());
        }
    }

}

//打印语句

test01线程悠闲级为:5
test02线程悠闲级为:8

2、优先级的规则性

CPU将尽量把执行资源给优先级越高的线程,看一下优先级不一样的线程执行结果怎么样:

public class Test01 {

    public static void main(String[] args) {
        //在启动之前分别设置两个线程的优先级
        test01 test01 = new test01();
        test01.setPriority(10);
        test01.start();
        test02 test02 = new test02();
        test02.setPriority(1);
        test02.start();
    }

    public static class test01 extends Thread {
        @Override
        public void run() {
            super.run();
            long beginTime = System.currentTimeMillis();
            long add = 0;
            for (int i = 0; i < 5000000; i++) {
                i++;
                Random random = new Random();
                random.nextInt();
                add = add + 1;
            }
            long endTime = System.currentTimeMillis();
            System.out.println("test01运行时间为:" + (endTime - beginTime) + "毫秒!");
        }
    }

    public static class test02 extends Thread {
        @Override
        public void run() {
            super.run();
            long beginTime = System.currentTimeMillis();
            long add = 0;
            for (int i = 0; i < 5000000; i++) {
                i++;
                Random random = new Random();
                random.nextInt();
                add = add + 1;
            }
            long endTime = System.currentTimeMillis();
            System.out.println("test02运行时间为:" + (endTime - beginTime) + "毫秒!");
        }
    }
}


//打印语句


test01运行时间为:297毫秒!
test02运行时间为:313毫秒!

Process finished with exit code 0

3、优先级的随机性:

  这个就简单了,上面我介绍过优先级的规则性,优先级越高的线程最先执行完,但是这个规则性并不是绝对的,当两个线程的优先级比较接近时,谁也不知道哪个线程最先执行完,这也就是线程的随机性,还有,不要把打印顺序当做线程执行完成的标准,这两个是无关的,看到这里是不是有些晕了,没错,我也晕,不过翻了好多相关的资料,都是这样讲解的。

最后,线程执行完成和打印出语句,他们的关系具有不确定性和随机性。

原文地址:https://www.cnblogs.com/itiande/p/9534475.html