初学者Javamultithreading编程:调度窗口和Mac之间的区别

背景

我是CS的学生,今天在课堂上我们学习了Java中的multithreading编程(Multi-Threaded Programming)。 教授要求学生编写一个简单的程序来演示线程的调度。 每个学生都有这样的代码:

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(); } } } 

期望

由于线程不一定在连续的时间内执行,我们期待线

线程i开始了

不严格限制在0,1,2 …到99的顺序。 相反,我们期待着类似的东西

线程0正在启动

线程5正在启动

线程2正在启动

…(九十行后)

线程95正在启动

线程99正在启动

线程98正在启动

(程序结束运行)

有趣的是,虽然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的行为有所不同? 这与使用的调度algorithm有关吗? 如果是这样,那么algorithm是什么?这会如何影响其他的JVMmultithreading程序?

为什么Mac和Windows的行为有所不同?

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

这与使用的调度算法有关吗?

创建一个线程是非常昂贵的,你的任务是非常短暂的,所以在这种情况下不太可能看到调度的效果。 真的,你正在测试如何创建线程。

如果是这样,那么算法是什么?这会如何影响其他的JVM多线程程序?

你需要看看操作系统的内部。

对编写良好的JVM程序的影响应该不会有任何影响。 您应该编写对特定操作系统或硬件操作细微差异不敏感的程序。

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

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

数字是在Windows而不是在Mac上订购的事实与操作系统中的调度程序有关。 从开发人员的角度来看,即使在Windows上看起来如此,也不应该依赖顺序。 在不同的负载下,或不同的体系结构(处理器上的核心/线程数,使用jvm),不能保证序列是正确的。