gpt4 book ai didi

java - 哪个先运行 : new thread or main thread?

转载 作者:行者123 更新时间:2023-12-01 07:05:57 26 4
gpt4 key购买 nike

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

//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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com