我运行了这段代码并得到了这个结果:
//back in main
//directly from the runnable
//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");
}
}
最佳答案
线程没有特定的顺序,可以是任意一种。 JVM 是决定这一点的人,它每次都会给出不同的结果。
关于java - 哪个先运行 : new thread or main thread?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24472154/