星空网 > 软件开发 > Java

java多线程系列8

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

在java中,线程的优先级分为1~10这10个等级,小于1或大于10,则会抛出IllegalArgumentException异常

JDK中使用3个常量来预定义优先级:

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

线程优先级的继承性

在java中,线程的优先级具有继承性,例如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

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

相关文章

java多线程系列8-线程的优先级

java多线程系列7-停止线程

java多线程系列6-阻塞队列

java多线程系列5-死锁与线程间通信

java多线程系列4-线程池

java多线程系列3-线程同步

java多线程系列2-线程控制

java多线程系列1--线程实现与调度




原标题:java多线程系列8

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

跨境物流有哪几种方式:https://www.goluckyvip.com/tag/93346.html
跨境电商物流渠道有哪些:https://www.goluckyvip.com/tag/93348.html
跨境物流的禁运产品有哪些:https://www.goluckyvip.com/tag/93349.html
黑五购物APP:https://www.goluckyvip.com/tag/9335.html
有哪些跨境物流:https://www.goluckyvip.com/tag/93351.html
跨境电子商务的物流模式有哪些:https://www.goluckyvip.com/tag/93352.html
海南岛琼海市旅游景点 琼海市的旅游景点:https://www.vstour.cn/a/363189.html
月活用户超20亿!万亿市值巨头对中国商家进一步开闸放流 :https://www.kjdsnews.com/a/1836412.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流