gpt4 book ai didi

java - 初学者 Java 多线程编程 : Scheduling Difference between Window and Mac

转载 作者:可可西里 更新时间:2023-11-01 14:14:38 26 4
gpt4 key购买 nike

背景

我是一名 CS 学生,今天在类里面我们学习了 Java 多线程编程。教授让学生写一个简单的程序来演示线程的调度。每个学生都有这样一段代码:

public class MyThread extends Thread {

private int num;

public MyThread(int num) {
this.num = num;
}

public void run() {
System.out.println("Thread " + num + " is starting.");
}

public static void main( String [] args ) {
for (int i=0; i<100; i++) {
MyThread mt = new MyThread(i);
mt.start();
}
}
}

期待

由于线程不一定按顺序执行,所以我们期待这条线

Thread i is starting

不要严格按照 0、1、2... 到 99 的顺序。相反,我们期待类似的东西

Thread 0 is starting

Thread 5 is starting

Thread 2 is starting

...(九十行之后)

Thread 95 is starting

Thread 99 is starting

Thread 98 is starting

(程序运行结束)

问题

有趣的是,虽然 Windows 用户似乎具有所描述的模式,但 Mac 用户在前 10 个线程中只有半随机顺序。之后,线程按照严格的升序执行,从 Thread 10 is startingThread 99 is starting

具体来说,Mac 输出如下所示:

Thread 0 is starting.
Thread 3 is starting.
Thread 2 is starting.
Thread 1 is starting.
Thread 5 is starting.
Thread 6 is starting.
Thread 4 is starting.
Thread 8 is starting.
Thread 7 is starting.
Thread 9 is starting.
Thread 10 is starting.
Thread 11 is starting.
Thread 12 is starting.
Thread 13 is starting.
Thread 14 is starting.
Thread 15 is starting.
Thread 16 is starting.
Thread 17 is starting.
Thread 18 is starting.
Thread 19 is starting.
Thread 20 is starting.
Thread 21 is starting.
Thread 22 is starting.
Thread 23 is starting.
Thread 24 is starting.
Thread 25 is starting.
Thread 26 is starting.
Thread 27 is starting.
Thread 28 is starting.
Thread 29 is starting.
Thread 30 is starting.
Thread 31 is starting.
Thread 32 is starting.
Thread 33 is starting.
Thread 34 is starting.
Thread 35 is starting.
Thread 36 is starting.
Thread 37 is starting.
Thread 38 is starting.
Thread 39 is starting.
Thread 40 is starting.
Thread 41 is starting.
Thread 42 is starting.
Thread 43 is starting.
Thread 44 is starting.
Thread 45 is starting.
Thread 46 is starting.
Thread 48 is starting.
Thread 49 is starting.
Thread 47 is starting.
Thread 50 is starting.
Thread 51 is starting.
Thread 52 is starting.
Thread 53 is starting.
Thread 54 is starting.
Thread 55 is starting.
Thread 56 is starting.
Thread 57 is starting.
Thread 58 is starting.
Thread 59 is starting.
Thread 60 is starting.
Thread 61 is starting.
Thread 62 is starting.
Thread 63 is starting.
Thread 64 is starting.
Thread 65 is starting.
Thread 66 is starting.
Thread 67 is starting.
Thread 68 is starting.
Thread 69 is starting.
Thread 70 is starting.
Thread 71 is starting.
Thread 72 is starting.
Thread 73 is starting.
Thread 74 is starting.
Thread 75 is starting.
Thread 76 is starting.
Thread 77 is starting.
Thread 78 is starting.
Thread 79 is starting.
Thread 80 is starting.
Thread 81 is starting.
Thread 82 is starting.
Thread 83 is starting.
Thread 84 is starting.
Thread 85 is starting.
Thread 86 is starting.
Thread 87 is starting.
Thread 88 is starting.
Thread 89 is starting.
Thread 90 is starting.
Thread 91 is starting.
Thread 92 is starting.
Thread 93 is starting.
Thread 94 is starting.
Thread 95 is starting.
Thread 96 is starting.
Thread 97 is starting.
Thread 98 is starting.
Thread 99 is starting.

为什么 Mac 和 Windows 的行为不同?这与使用的调度算法有关吗?如果是这样,算法是什么?这会如何影响其他 JVM 多线程程序?

最佳答案

Why do Mac and Windows behave differently?

线程调度是基于操作系统内部的。您只能说 Mac OSX 和 Windows 的行为不同,因为它们是不同的操作系统。如果您有不同的硬件,这也会有所不同。

Is this related to the scheduling algorithms used?

创建一个线程是非常昂贵的,而且你的任务是非常短暂的,所以在这种情况下你不太可能看到调度的效果。实际上,您是在测试线程的创建方式。

If so, what are the algorithms and how might this affect other JVM Multi-threaded programs?

您需要查看操作系统的内部结构。

对编写良好的 JVM 程序的影响应该完全没有。您应该编写对特定操作系统或硬件运行方式的细微差异不敏感的程序。

你也不应该疯狂地创建线程,这显然是低效的。推测一个写得不好或效率低下的程序是如何运行的并不是很有用。

我建议从一个使用多个线程比使用一个线程执行得更好的问题开始(在您的示例中,一个线程会更有效)并查看调度是否对一系列 CPU 类型有影响。

关于java - 初学者 Java 多线程编程 : Scheduling Difference between Window and Mac,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28752003/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com