线程优先级设置: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中使用三个预定义变量来定义优先级:线程的优先级具有继承性,例如A线程启动B线程,则A和B的优先级是一样的,代码如下:public final static int MIN_PRIORITY = 1; public final static int NORM_PRIORITY = 5; public final static int MAX_PRIORITY = 10; 每个线程都有优先级,优先级默认值是5
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
线程的优先级与打印顺序无关,它们的关系具有不确定性和随机性