典型的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应用程序生命周期
发布评论