线程优先级

线程优先级设置:setPriority(int  value) 括号参数是1-10级别。1代表最低级别,10代表最高级别。小于1或大于10,会抛出IllegalArgumentException异常。

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


在JDK中使用三个预定义变量来定义优先级:
public final static int MIN_PRIORITY = 1;
public final static int NORM_PRIORITY = 5;
public final static int MAX_PRIORITY = 10;

每个线程都有优先级,优先级默认值是5

线程的优先级具有继承性,例如A线程启动B线程,则A和B的优先级是一样的,代码如下:

public class MyThread1 extends Thread {
    @Override
    public void run() {
        System.out.println("MyThread1 run priority=" + this.getPriority());
        MyThread2 thread2 = new MyThread2();
        thread2.start();
    }
}
public class MyThread2 extends Thread {
    @Override
    public void run() {
        System.out.println("MyThread2 run priority=" + this.getPriority());
    }
}
public class Run {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("main thread begin priority="
                + Thread.currentThread().getPriority());
        System.out.println("main thread end priority="
                + Thread.currentThread().getPriority());
        MyThread1 thread1 = new MyThread1();
        thread1.start();
    }
}

运行结果如下:

main thread begin priority=5
main thread end priority=5
MyThread1 run priority=5
MyThread2 run priority=5



优先级具有规则性

虽然setPriority()方法可以设置线程的优先级,但是没有看到设置优先级所带来的效果

举例如下:

public class MyThread1 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        long addResult = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 50000; j++) {
                Random random = new Random();
                random.nextInt();
                addResult += j;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("* * * * * thread 1 use time=" + (endTime - beginTime));
    }
}
public class MyThread2 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        long addResult = 0;
        for (int i = 0; i < 10; i++) {
            for (int j = 0; j < 50000; j++) {
                Random random = new Random();
                random.nextInt();
                addResult += j;
            }
        }
        long endTime = System.currentTimeMillis();
        System.out.println("* * * * * thread 2 use time=" + (endTime - beginTime));
    }
}
public class Run {
    public static void main(String[] args) throws InterruptedException {
        
        for(int i=0;i<5;i++){
            MyThread1 thread1=new MyThread1();
            thread1.setPriority(10);
            thread1.start();
            MyThread2 thread2 =new MyThread2();
            thread2.setPriority(1);
            thread2.start();
        }
    }
}

运行结果如下:

* * * * * thread 1 use time=174
* * * * * thread 1 use time=221
* * * * * thread 1 use time=224
* * * * * thread 2 use time=360
* * * * * thread 1 use time=202
* * * * * thread 2 use time=185
* * * * * thread 1 use time=169
* * * * * thread 2 use time=466
* * * * * thread 2 use time=425
* * * * * thread 2 use time=98

从上面的结果可以看出,高优先级的线程总是大部分先执行完,但是不是所有的先执行完。先执行完也不是因为先调用,如果更改优先级,先执行完和和代码的调用顺序无关。

优先级具有一定的规则性,CPU总是尽量将执行资源让给优先级比较高的线程

优先级具有随机性

优先级较高的线程不一定每一次都先执行完,举个例子:

public class MyThread1 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            Random random = new Random();
            random.nextInt();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("* * * * * thread 1 use time="
                + (endTime - beginTime));
    }
}
public class MyThread2 extends Thread {
    @Override
    public void run() {
        long beginTime = System.currentTimeMillis();
        for (int i = 0; i < 1000; i++) {
            Random random = new Random();
            random.nextInt();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("* * * * * thread 2 use time="
                + (endTime - beginTime));
    }
}
public class Run {
    public static void main(String[] args) throws InterruptedException {
        for (int i = 0; i < 5; i++) {
            MyThread1 thread1 = new MyThread1();
            thread1.setPriority(5);
            thread1.start();
            MyThread2 thread2 = new MyThread2();
            thread2.setPriority(6);
            thread2.start();
        }
    }
}

运行结果如下:

* * * * * thread 1 use time=5
* * * * * thread 2 use time=4
* * * * * thread 1 use time=5
* * * * * thread 1 use time=4
* * * * * thread 1 use time=4
* * * * * thread 2 use time=6
* * * * * thread 2 use time=3
* * * * * thread 2 use time=5
* * * * * thread 1 use time=2
* * * * * thread 2 use time=6

线程的优先级与打印顺序无关,它们的关系具有不确定性和随机性




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值