Java多线程编程初学者:Windows和Mac之间的调度差异 背景

Java多线程编程初学者:Windows和Mac之间的调度差异 背景,java,windows,multithreading,macos,Java,Windows,Multithreading,Macos,我是一名CS学生,今天在课堂上我们学习了Java多线程编程。教授要求学生编写一个简单的程序来演示线程的调度。每个学生都有这样一段代码: public class MyThread extends Thread { private int num; public MyThread(int num) { this.num = num; } public void run() { System.out.println("T

我是一名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();
        }
    }
}

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

在Windows上而不是Mac上排序的数字与操作系统中的调度程序有关。从开发人员的角度来看,您不应该依赖于序列为真,即使在Windows上看起来是这样。在不同的负载或不同的体系结构(处理器上的内核/线程数量、使用的jvm)下,无法保证序列是正确的

为什么Mac和Windows表现不同

线程调度基于操作系统的内部结构。你所能说的就是MacOSX和Windows由于是不同的操作系统而表现不同。如果你有不同的硬件,这也会有不同

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

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

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

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

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

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


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

尝试将输出重定向到文件或在IDE外部/内部运行。有趣的是,当我在Eclipse和命令行中运行时,行为是相似的(前10个是随机的,然后是严格的升序)。下次我有机会的话,我会尝试在Windows上做。
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.