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 ...
随机推荐
- 资源 | 数十种TensorFlow实现案例汇集:代码+笔记
选自 Github 机器之心编译 参与:吴攀.李亚洲 这是使用 TensorFlow 实现流行的机器学习算法的教程汇集.本汇集的目标是让读者可以轻松通过案例深入 TensorFlow. 这些案例适合那 ...
- JS算法总结
1.选择排序: var arr = [3,6,7,2,6,4,1,6,8,24,12,53]; function sort(arr){ // 当数组的长度小于1的时候结束递归 if(arr.lengt ...
- Unity 中的协同程序
今天咱就说说,协同程序coroutine.(这文章是在网吧敲的,没有unity,但是所有结论都被跑过,不管你信得过我还是信不过我,都要自己跑一下看看,同时欢迎纠错)先说说啥是协程:协同程序是一个非常让 ...
- mysql数据库事件调度(Event)
mysql中的事件调度器可以定时对数据库增加,删除和执行操作,相当于数据库中的临时触发器,与Linux系统中的执行计划任务一样,这样就可以大大降低工作量. 1.开启事件调度器 [root@node1 ...
- codeforces MUH and Cube Walls
题意:给定两个序列a ,b, 如果在a中存在一段连续的序列使得 a[i]-b[0]==k, a[i+1]-b[1]==k.... a[i+n-1]-b[n-1]==k 就说b串在a串中出现过!最后输出 ...
- JavaScript的深拷贝的实现
JavaScript的数据类型 简单数据类型 string number boolean function null undefined 复杂数据类型 String Number Boolean Fu ...
- FAQ: Machine Learning: What and How
What: 就是将统计学算法作为理论,计算机作为工具,解决问题.statistic Algorithm. How: 如何成为菜鸟一枚? http://www.quora.com/How-can-a-b ...
- Tips10:你可以像写文档一样自由的复制粘贴游戏组件(Game Object Component)
相对于添加组件后再进行调整和赋值,通过复制和粘贴已有游戏组件能够带来更高的效率.
- Robot Framework自动化测试(四)--- 分层思想
谈到Robot Framework 分层的思想,就不得不提“关键字驱动”. 关键字驱动: 通过调用的关键字不同,从而引起测试结果的不同. 在上一节的selenium API 中所介绍的方法其实就是关 ...
- [Solution] 一步一步WCF(1) 快速入门
Windows Communication Foundation(WCF)是由微软开发的一系列支持数据通信的应用程序框架,可以翻译为Windows 通讯开发平台.整合了原有的windows通讯的 .n ...