Beginner Java Multi-Threaded Programming: Scheduling Difference between Window and Mac

java, macos, multithreading, windows

Solution

Why do Mac and Windows behave differently?

The thread scheduling is based on the internals of the operating system. All you can say is that Mac OSX and Windows behave differently as they are different operating systems. if you have different hardware, this will also make a difference.

Is this related to the scheduling algorithms used?

Creating a thread is very expensive and your task is very short lived so it is unlikely you will see the effect of scheduling in this case. really you are testing how threads are created.

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

You need to look at the internals of the OS.

The effect on a well written JVM program should none at all. You should write programs which are not sensitive to subtle differences in how a particular OS or hardware operates.

You also shouldn't create threads like mad and this is obviously inefficient. Speculating on how a poorly written, or inefficient program operates isn't very useful.

I suggest starting with a problem which performs better with multiple threads than with one (in your example one thread would be more efficient) and see if scheduling has an impact on a range of CPU types.

Problem

Background I am a CS student and today in class we learned Multi-Threaded Programming in Java. The professor asked students to write a simple program to demonstrate the scheduling of threads. Every student had this piece of code: ``` 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(); } } } ``` Expectation Since threads don't necessarily execute in sequential time, we were expecting the line Thread `i` is starting to not be in strictly 0, 1, 2... to 99 order. Instead, we were expecting something like Thread 0 is starting Thread 5 is starting Thread 2 is starting ... (ninety lines later) Thread 95 is starting Thread 99 is starting Thread 98 is starting (Program finishes running) Question Interestingly, while Windows users seem to have the pattern as described, Mac users only have semi-random order in the first 10 threads. Afterwards, the threads execute in strictly ascending order, from `Thread 10 is starting` up to `Thread 99 is starting`. Specifically, Mac output looks like this: ``` 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. ``` Why do Mac and Windows behave differently? Is this related to the scheduling algorithms used? If so, what are the algorithms and how might this affect other JVM Multi-threaded programs?

Original source