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 ...
随机推荐
- SQL语句中将Datetime类型转换为字符串类型
0 Feb 22 2006 4:26PM CONVERT(CHAR(19), CURRENT_TIMESTAMP, 0) 1 02/22/06 CONVERT(CHAR(8), CURRENT_ ...
- MySQL单机load过高问题讨论
有一个朋友问我: "hi,我想问下你们遇到单机load过高的情况 采取什么紧急措施啊?" 我问他是不是mysql db server? 他说是. 我给他如下建议: 1 先看下是不是 ...
- js中加密及设置cookie
1.设置cookie及有效期时长 //cname:cookie的名称,cvalue:cookie的内容,exdays:cookie有效期时长: function setCookie(cname, cv ...
- Ladda – 把加载提示效果集成到按钮中,提升用户体验
Ladda 是一组集成了加载提示的按钮,以弥合行动和反馈之间的时间间隔,提供更好的功能使用体验.主要用于在用户点击提交之后,向用户提供即时的反馈,让他们知道浏览器正在处用户提交的任务. 您可能感兴趣的 ...
- Android学习笔记之使用百度地图实现Poi搜索
PS:装个系统装了一天.心力憔悴.感觉不会再爱了. 学习内容: 1.使用百度Map实现Poi搜索. 2.短串分享 3.在线建议查询 百度地图的研究也算是过半了.能够实现定位,实现相关信息的搜索,实 ...
- js代码中的闭包
作为一个后台开发人员了解前端非常重要,尤其是深处学校实验室做项目时前端把写好的代码直接给你,然后你在修改的时候.我经常做的就是修改前端的代码的HTML和后台交互的部分以及js的ajax部分,之后修改之 ...
- Linux永久修改系统时间和时区方法
修改时区: 1> 找到相应的时区文件 /usr/share/zoneinfo/Asia/Shanghai 用这个文件替换当前的/etc/localtime文件. 或者找你认为是标准时间的服务器, ...
- [Architect] Abp 框架原理解析(5) UnitOfWork
本节目录 介绍 分析Abp源码 实现UOW 介绍 UOW(全称UnitOfWork)是指工作单元. 在Abp中,工作单元对于仓储和应用服务方法默认开启.并在一次请求中,共享同一个工作单元. 同时在Ab ...
- 实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)
实现虚拟模式的即时数据加载Windows窗体DataGridView控件 .net 4.5 原文地址 :http://msdn.microsoft.com/en-us/library/ms171624 ...
- ADO.NET 增 删 改 查
ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...