Java应用程序生命周期 [英] Java app lifecycle

查看:296
本文介绍了Java应用程序生命周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

典型的Java应用何时完成?

如果我在main方法中启动一个新线程,然后main方法完成,但另一个线程继续工作,则该应用程序仍将处于打开状态,直到所有线程都消失为止,不是吗? /p>

感谢&圣诞快乐!

解决方案

是的,除非它是守护进程线程.引用线程API :

当Java虚拟机启动时,通常只有一个非守护程序线程(通常调用某些指定类的名为main的方法). Java虚拟机将继续执行线程,直到发生以下两种情况之一为止:

  • 已调用Runtime类的退出方法,并且安全管理器已允许进行退出操作.
  • 不是守护程序线程的所有线程都已死亡,要么通过从调用返回到run方法,要么抛出传播到run方法之外的异常.

When does a typical Java app finish?

If I start a new thread in the main method and then the main method finishes, but the other thread continues working, the app would still be on until all it's threads have died, wouldn't it?

Thanks & Merry Christmas!

解决方案

Yes, unless it's a deamon thread. Quoting from Thread API:

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.

这篇关于Java应用程序生命周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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