

[Java 并发]关于线程优先级的相关问题,看完这篇文章就明白了
source link: https://www.dynamic-zheng.com/posts/cddfeba2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

[Java 并发]关于线程优先级的相关问题,看完这篇文章就明白了
线程的优先级是可以被定义的
通过源码我们能够看到:
public final void setPriority(int newPriority) {
ThreadGroup g;
checkAccess();
// 如果设置的 priority 大于系统设置的最大值,或者小于系统设置的最小值
// 抛出异常
if (newPriority > MAX_PRIORITY || newPriority < MIN_PRIORITY) {
throw new IllegalArgumentException();
}
if((g = getThreadGroup()) != null) {
// 如果设置线程的优先级大于线程组的优先级,则重置线程的优先级为线程组的优先级
if (newPriority > g.getMaxPriority()) {
newPriority = g.getMaxPriority();
}
setPriority0(priority = newPriority);
}
}
一个 Thread 必然存在于一个 ThreadGroup , Thread 有自己的优先级, ThreadGroup 也有优先级,如果 Thread 的优先级大于 ThreadGroup 的优先级了,那么就以 ThreadGroup 的优先级为准
接下来咱们聊聊关于线程优先级的其他问题
线程优先级是否具有继承性?
如果在线程 A 中启动了线程 B ,此时线程 A 的优先级为 5 , 那么线程 B 的优先级是否也是 5 呢?
答案是: 是的,线程 B 会继承线程 A 的优先级,也就是此时线程 B 的优先级就是 5 .即: 线程优先级具有继承性
我们具体来看例子,具体代码如下:
/**
* @introduce: 验证线程优先级具有继承性
* @author: zll
* @DATE: 2021-3-14 12:09:51
**/
public class ThreadInheritOne extends Thread{
@Override
public void run(){
System.out.println("ThreadInheritOne Priority is " + this.getPriority());
ThreadInheritTwo threadInheritTwo = new ThreadInheritTwo();
threadInheritTwo.start();
}
}
/**
* @introduce: 验证线程优先级具有继承性
* @author: zll
* @DATE: 2021-3-14 12:09:51
**/
public class ThreadInheritTwo extends Thread{
@Override
public void run(){
System.out.println("ThreadInheritTwo Priority is " + this.getPriority());
}
}
/**
* @introduce: 验证线程优先级具有继承性
* @author: zll
* @DATE: 2021-3-14 12:09:51
**/
public class ThreadInheritRun {
public static void main(String[] args) {
System.out.println("main thread start, its priority is " + Thread.currentThread().getPriority());
Thread.currentThread().setPriority(10);
System.out.println("main thread end, its priority is " + Thread.currentThread().getPriority());
ThreadInheritOne threadInheritOne = new ThreadInheritOne();
threadInheritOne.start();
}
}
运行一下以上代码,执行结果如下:
我们能够看到,本来运行的 main 线程优先级是 5 ,后来设置成 10 之后,再在里面启动 ThreadInheritOne
线程,此时它的线程优先级也变成了 10 , ThreadInheritOne
线程里又启动了 ThreadInheritTwo
,此时 ThreadInheritTwo
的线程优先级也是 10
由此,可以验证线程优先级具有继承性
设置线程优先级具有什么意义?
设置线程优先级,我们是肯定希望这个线程相对于其他线程,能够优先被 CPU 执行的,那么这一点怎么验证下呢?
还是来看个例子:
/**
* @introduce: 验证线程优先级高的优先被 CPU 执行
* @author: zll
* @DATE: 2021-3-14 12:26:12
**/
public class ThreadPriorityOne extends Thread{
@Override
public void run(){
long beginTime = System.currentTimeMillis();
long result = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 50000; j++) {
Random random = new Random();
random.nextInt();
result = result + j;
}
}
long endTime = System.currentTimeMillis();
System.out.println("★★★★★ threadOne priority is relatively high , use time = " + (endTime - beginTime));
}
}
/**
* @introduce: 验证线程优先级高的优先被 CPU 执行
* @author: zll
* @DATE: 2021-3-14 12:26:12
**/
public class ThreadPriorityTwo extends Thread{
@Override
public void run(){
long beginTime = System.currentTimeMillis();
long result = 0;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 50000; j++) {
Random random = new Random();
random.nextInt();
result = result + j;
}
}
long endTime = System.currentTimeMillis();
System.out.println("☆☆☆☆☆ threadTwo priority is relatively low , use time = " + (endTime - beginTime));
}
}
/**
* @introduce: 验证线程优先级高的优先被 CPU 执行
* @author: zll
* @DATE: 2021-3-14 12:26:12
**/
public class ThreadPriorityRun {
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
ThreadPriorityOne threadOne = new ThreadPriorityOne();
threadOne.setPriority(10);
threadOne.start();
ThreadPriorityTwo threadTwo = new ThreadPriorityTwo();
threadTwo.setPriority(1);
threadTwo.start();
}
}
}
来看下代码的运行效果:
上面的截图只是一部分,不过也能让我们发现规律了,就是: 线程优先级高的,会优先被 CPU 执行
你可能会说了,那是当然的,因为在 main 线程中,将 ThreadPriorityOne
线程写在了 ThreadPriorityTwo
线程前面,根据线程调用顺序,此时 main 线程中肯定先执行 ThreadPriorityOne
线程,那么 ThreadPriorityOne
线程优先被执行完也就不足为奇了
那我们将这两个线程的优先级换一下,看看效果(所以代码有些微调整,也就是将 ThreadPriorityOne
的优先级调整为 1 , ThreadPriorityTwo
线程优先级调整为 10 ,相应的文案也进行了调整)
可以看到,尽管 main 线程先调用了 ThreadPriorityOne
线程,但是 ThreadPriorityTwo
线程还是优先被执行了
也就是:线程优先级比较高的话,就会被 CPU 优先执行,和此时 main 线程先调用哪儿个线程没有关系
- Java 多线程编程核心技术
以上,
感谢您的阅读~
Recommend
-
51
-
45
前言 Java 的线程的调度机制由 JVM 实现,假如有若干条线程,你想让某些线程拥有更长的执行时间,或某些线程分配少点执行时间,这时就涉及“线程优先级”。 优先级别 ...
-
29
-
45
Java 多线程系列第 6 篇。 这篇我们来看看 Java 线程的优先级。 Java 线程优先级 Thread 类中,使...
-
15
Overview最近重新阅读了
-
20
经过前面几节的介绍,相信读者在C语言程序开发中,进行多线程编程已经不是什么难事了。以 Linux 下的开发来说,其实就是 pthread 库的应用而已。多个线程是“同时运行”的吗?在之前的文章中,我们提到在较大的C语言项目中,为了不阻塞主逻辑,比...
-
11
"协程是轻量级的线程",相信大家不止一次听到这种说法。但是您真的理解其中的含义吗?恐怕答案是否定的。接下来的内容会告诉大家协程是如何在 Android 运行时中被运行的,它们和线程之间的关系是什么,以及在使用 Java 编程...
-
6
清晰、高效、一致、美观 - 关于设计原则的优先级排序 首先,向在上一篇念叨当中留言评论过的所有朋友真挚的说一声谢谢。你们...
-
6
1.环境介绍 6.对比java实现 废话就不多说了,本文分享下博主在5.28大促压测期间解决的一个性能问题,觉得这个还是比较有意思的,值得总结拿出来分享下。 博主所服务的部门是作为公共业务平台,公共业务平台支持上层所有业务系统(2C、...
-
3
计划写几篇文章讲述下Java并发编程,帮助一些初学者成体系的理解并发编程并实际使用,而不只是碎片化的了解一些Synchronized、ReentrantLock等技术点。在讲述的过程中,也想融入一些相关技术、概念的发展历史,这样便于看到其演化过程而更好地进行理解。文字描述上希...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK