java – 终止程序在主线程退出?

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

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

最佳答案
没有.

所有非守护进程线程完成后,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 – 终止程序在主线程退出? - 代码日志