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多线程系列--“JUC线程池”03之 线程池原理(二)
概要 在前面一章"Java多线程系列--“JUC线程池”02之 线程池原理(一)"中介绍了线程池的数据结构,本章会通过分析线程池的源码,对线程池进行说明.内容包括:线程池示例参考代 ...
- Java多线程系列--“JUC线程池”06之 Callable和Future
概要 本章介绍线程池中的Callable和Future.Callable 和 Future 简介示例和源码分析(基于JDK1.7.0_40) 转载请注明出处:http://www.cnblogs.co ...
- Java多线程系列--“JUC线程池”02之 线程池原理(一)
概要 在上一章"Java多线程系列--“JUC线程池”01之 线程池架构"中,我们了解了线程池的架构.线程池的实现类是ThreadPoolExecutor类.本章,我们通过分析Th ...
- Java多线程系列--“JUC线程池”04之 线程池原理(三)
转载请注明出处:http://www.cnblogs.com/skywang12345/p/3509960.html 本章介绍线程池的生命周期.在"Java多线程系列--“基础篇”01之 基 ...
- Java多线程系列--“JUC线程池”05之 线程池原理(四)
概要 本章介绍线程池的拒绝策略.内容包括:拒绝策略介绍拒绝策略对比和示例 转载请注明出处:http://www.cnblogs.com/skywang12345/p/3512947.html 拒绝策略 ...
- java多线程系列(六)---线程池原理及其使用
线程池 前言:如有不正确的地方,还望指正. 目录 认识cpu.核心与线程 java多线程系列(一)之java多线程技能 java多线程系列(二)之对象变量的并发访问 java多线程系列(三)之等待通知 ...
- Java多线程系列--“JUC线程池”01之 线程池架构
概要 前面分别介绍了"Java多线程基础"."JUC原子类"和"JUC锁".本章介绍JUC的最后一部分的内容——线程池.内容包括:线程池架构 ...
- Java多线程系列 JUC线程池06 线程池原理解析(五)
ScheduledThreadPoolExecutor解析 ScheduledThreadPoolExecutor适用于延时执行,或者周期性执行的任务调度,ScheduledThreadPoolExe ...
- Java多线程系列 JUC线程池04 线程池原理解析(三)
转载 http://www.cnblogs.com/skywang12345/p/3509954.html https://blog.csdn.net/qq_22929803/article/det ...
随机推荐
- [转]C# dataGridview 报“索引-1没有值”的解决办法
很多WINFORM的开发人员在DataGridView的开发当中,都会出现“索引-1没有值”这个烦人的问题,其实较早之前,我已经大概知道问题的所在,也找到了解决方法,不过一直没有时间去深入研究一下,今 ...
- Shader的语法
Shader "name" { [Properties] Subshaders [Fallback] }(1)Properties:{ Property [Property ... ...
- 【Android菜鸟学习之路】环境搭建问题-修改AVD Path
更改avd默认路径
- 想从事分布式系统,计算,hadoop等方面,需要哪些基础,推荐哪些书籍?--转自知乎
作者:廖君链接:https://www.zhihu.com/question/19868791/answer/88873783来源:知乎 分布式系统(Distributed System)资料 < ...
- Love
愿这段代码陪我走过此生,献给我最爱的榨菜. /** *@Description:<p>我爱榨菜</p> *@author 王旭 *@time 2016年4月25日 下午7:58 ...
- Android学习笔记之使用LBS实现定位
PS:最近一直在搞使用LBS实现定位.一般现在涉及到日常生活交易平台的app.貌似都需要使用定位.比如说美团外卖,我请客等app. 学习内容: 1.LBS定位的简单介绍. 2.在Map上添加地图覆盖物 ...
- Entity Framework优缺点及使用方法总结
Entity Framework是M$提供的一个ORM框架,它旨在为小型应用程序中数据层的快速开发提供便利. nuget上185W多的下载量,说明.Net开发人员还是比较喜欢用EF的.但是EF在提供了 ...
- 如何彻底的卸载和删除Windows service
最近遇到很头疼的问题,安装到服务器的Windows Service卸载的时候出错了,结果在服务列表中就一直驻留,并且系统进程一直在运行,怎么都杀不掉. 最后终于找到办法了: 1.常规做法,批处理命令卸 ...
- Android开发权威指南(第2版)新书发布
<Android 开发权威指南(第二版)>是畅销书<Android开发权威指南>的升级版,内容更新超过80%,是一本全面介绍Android应用开发的专著,拥有45 章精彩内容供 ...
- Linq查询操作之投影操作
投影操作,乍一看不知道在说啥.那么什么是投影操作呢?其实就是Select操作,名字起的怪怪的.和Linq查询表达式中的select操作是一样的.它能够选择数据源中的元素,并指定元素的表现形式.投影操作 ...