gpt4 book ai didi

java - 一个线程总是在没有机会运行其他线程

转载 作者:行者123 更新时间:2023-11-29 09:46:06 24 4
gpt4 key购买 nike

为什么主线程从不执行?我以为那是我使用 Thread.sleep(int value) 我给其他线程一个运行的机会,但这从来没有发生过。

    public static void main(String[] args) {
final Sook o = new Sook();
Thread t = new Thread(new Runnable() {
public void run() {
while (true) {
try {
Thread.sleep(10000); // Specially set to give a chance to the main thread to run
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});

t.run();

System.out.println("<<<<<BACK TO MAIN >>>>>>"); // Never happens
}

最佳答案

不调用,t.run(),调用t.start()

Just run会调用当前Thread中的run方法。

关于java - 一个线程总是在没有机会运行其他线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4808443/

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