gpt4 book ai didi

java - 主线程退出时程序终止?

转载 作者:行者123 更新时间:2023-12-01 19:33:13 25 4
gpt4 key购买 nike

我有两个线程:主线程和从主线程生成的线程。

当主线程退出时,整个程序会终止吗?

最佳答案

没有。

当所有非守护线程完成时,Java 程序终止。

documentation状态:

When a Java Virtual Machine starts up, there is usually a single non-daemon thread (which typically calls the method named main of some designated class). The Java Virtual Machine continues to execute threads until either of the following occurs:

  • The exit method of class Runtime has been called and the security manager has permitted the exit operation to take place.
  • All threads that are not daemon threads have died, either by returning from the call to the run method or by throwing an exception that propagates beyond the run method.

如果您不希望运行时等待线程,请调用 setDaemon method .

关于java - 主线程退出时程序终止?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59243441/

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