主线程退出程序终止? [英] termination of program on main thread exit?

查看:135
本文介绍了主线程退出程序终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have two threads: the main thread and a thread generated from the main thread.

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

When the main thread exits, will the whole program terminate?

推荐答案

否。

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

Java programs terminate when all non-daemon threads finish.

文档声明:


当Java虚拟机启动时,
通常只有一个非-daemon
thread(通常调用名为main的某个指定
类的
方法)。 Java虚拟机
继续执行线程,直到
发生以下任一情况:

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:


  • 退出类的方法运行时已被调用且
    安全管理器已允许
    退出操作发生。

  • 所有不是守护程序线程的线程都已经死亡,要么通过调用 run 方法返回
    ,要么
    抛出
    传播超出 run 方法的异常。

  • 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 方法

If you don't want the runtime to wait for a thread, call the setDaemon method.

这篇关于主线程退出程序终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆