Java应用程序生命周期

编程入门 行业动态 更新时间:2024-10-28 20:21:54
本文介绍了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应用程序生命周期

本文发布于:2023-11-17 04:16:44,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1608795.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   生命周期   Java

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!