哪个先运行:新线程还是主线程?

Yar*_*Yar 0 java scheduler

我运行了这段代码并得到了以下结果:

//back in main
//directly from the runnable
//top of the stack
Run Code Online (Sandbox Code Playgroud)

我不应该top of the stack作为第一个结果吗?

public class MyRunnable implements Runnable {
    public void run() {
        System.out.println("directly from the runnable");
        go();
    }
    public void go() {
        doMore();
    }
    public void doMore() {
        System.out.println("top of the stack");
    }
}

public class ThreadTester{

    public static void main(String[] args) {
        Runnable threadJob = new MyRunnable();
        Thread myThread = new Thread(threadJob);

        myThread.start();

        System.out.println("back in main");
    }

}
Run Code Online (Sandbox Code Playgroud)

小智 5

线程没有特定的顺序,可以是任何一种方式。JVM是决定这一点的,它每次都能给出不同的结果。