gpt4 book ai didi

Java "unstopped "执行/完成线程

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:18:12 26 4
gpt4 key购买 nike

我有一个关于线程的问题。当我这样做时:

new Thread(new Runnable(){

@Override
public void run() {
//sth to do
}

}).start();

当 run() 中的所有代码都被执行时会发生什么?是系统自动删除线程还是线程还留在内存中?

谢谢和问候

最佳答案

当线程完成其run() 方法时,它将进入'死' 状态。然后堆栈中的下一个线程在之后运行。

死状态:

"A thread is considered dead when its run() method completes. It may still be a viable Thread object, but it is no longer a separate thread of execution. Once a thread is dead, it can never be brought back to life! (The whole "I see dead threads" thing.) If you invoke start() on a dead Thread instance, you'll get a runtime (not compiler) exception. And it probably doesn't take a rocket scientist to tell you that if a thread is dead, it is no longer considered to be alive."

关于Java "unstopped "执行/完成线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16507877/

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