java - 运行通过扩展 Thread 类创建的 3 个线程的程序的输出
问题描述
我是 Java 多线程的新手,并试图找出创建 3 个线程 a、b 和 c 的以下代码的输出。
class A extends Thread {
int i = 0;
public void run() {
System.out.println("Thread A started");
while (i < 4) {
System.out.println("\t value of i in Thread A:" + i);
i++;
}
System.out.println("ThreadA finished");
}
}
class B extends Thread {
public void run() {
int i = 0;
System.out.println("ThreadB started");
while (i < 4) {
System.out.println("\t value of i in Thread B:" + i);
i++;
}
System.out.println("ThreadB finished");
}
}
class C extends Thread {
public void run() {
int i = 0;
System.out.println("ThreadC started");
while (i < 4) {
System.out.println("\t value of i in Thread C" + i);
i++;
}
System.out.println("ThreadC finished");
}
}
public class App {
public static void main(String[] args) throws Exception {
// System.out.println("Hello, World!");
System.out.println("Main Thread started");
A a = new A();
B b = new B();
C c = new C();
Thread th = Thread.currentThread();
System.out.println(th.getName());
System.out.println(th.getPriority());
System.out.println("Priority of A thread " + a.getPriority());
System.out.println("Priority of B thread " + b.getPriority());
System.out.println("Priority of C thread " + c.getPriority());
System.out.println();
th.setPriority(Thread.MAX_PRIORITY);
b.setPriority(Thread.MIN_PRIORITY);
c.setPriority(Thread.NORM_PRIORITY);
System.out.println("Showing new Priorites \n");
System.out.println("Priority of TH thread " + th.getPriority());
System.out.println("Priority of A thread " + a.getPriority());
System.out.println("Priority of B thread " + b.getPriority());
System.out.println("Priority of C thread " + c.getPriority());
System.out.println();
a.start();
b.start();
c.start();
}
}
上面代码的输出是
Main Thread started
main
5
Priority of A thread 5
Priority of B thread 5
Priority of C thread 5
Showing new Priorites
Priority of TH thread 10
Priority of A thread 5
Priority of B thread 1
Priority of C thread 5
ThreadC started
value of i in Thread C0
Thread A started
value of i in Thread A:0
value of i in Thread A:1
value of i in Thread C1
value of i in Thread C2
value of i in Thread C3
ThreadC finished
ThreadB started
value of i in Thread A:2
value of i in Thread A:3
ThreadA finished
value of i in Thread B:0
value of i in Thread B:1
value of i in Thread B:2
value of i in Thread B:3
ThreadB finished
我知道在调用 start() 方法之后,线程变得可运行并准备好被线程调度程序选择。
从输出中我们可以看到线程 C 首先开始运行它的 run() 方法,然后它应该已经完成了它的 run() 方法。但是在线程 C 的 run() 方法完成之前,线程 a 的 run() 方法是如何执行的呢?有人请在这里帮忙。
解决方案
您不应该依赖线程优先级。这是Oracle JRockit 文档所说的:
不要依赖线程优先级
使用时要小心
java.lang.Thread.setPriority
。取决于线程优先级可能会导致不希望的或意外的结果,因为调度算法可能会选择使 CPU 时间的低优先级线程饿死并且从不执行它们。此外,操作系统和 JVM 之间的结果可能不同。Java API 规范声明“每个线程都有一个优先级。优先级较高的线程优先于较低优先级的线程执行。”
该
setPriority()
方法设置的优先级是线程调度算法中可能使用的参数,它在执行线程之间共享 CPU 执行时间。该算法可能由 JVM 或操作系统控制。重要的是要意识到该算法通常在操作系统之间有所不同,并且该算法可能会在操作系统和 JVM 的版本之间发生变化。对于 BEA JRockit JVM 原生线程,算法由操作系统实现。
您观察到的是线程调度的系统特定行为的影响......如上述文档所述(例如)。
推荐阅读
- python - 插入 Postgresql 表时字符串索引超出范围
- c++ - 即使第一个不为真,嵌套的 if 循环也会跳到第二个
- python - 如何测试一个对象是否是锁?
- javascript - 如何在缩放时将容器内的元素保持填充父容器
- postgresql - 在哪里可以找到有关 PostgreSQL 中复制槽选项的完整列表?
- css - 在纯 CSS 中为元素添加图像背景大小动画效果
- firebase - 错误:来自 google.com 的 OAuth 响应无效
- laravel - 如何强制 laravel 在我的翻译文件夹中使用 en.json 文件?
- python - python request.post,向服务器发布用户评论
- python-3.x - 如何从python3中的同级目录导入?