I know that finally blocks in deamon threads would not be executed. But my meticulous nature tries to understand why and what happens in JVM so special that it could not call the code under this block.
I think that it somehow related to call stack that it whould not unwind, but don’t know how. Can someone please shed some light on this.
Thanks.
Who says that
finallyblocks in daemon threads don’t execute? This is not true in general.What you might have heard that a
finallyblock is not guaranteed to be executed when a JVM is shut down during the execution of thetry(orcatch) block. That is correct (and it can easily happen to daemon threads).But again: during normal operation, there is nothing that stops
finallyblocks from executing normally in daemon threads: they are not handled differently.The shutdown problem is easy: when the JVM is asked to shut down or even forced to shut down, then it may simply not be able to execute any more statements.
For example, on POSIX-y operating systems, signal 9 (SIGKILL) forces an application to quit, giving it no chance to do any cleanup (this is why signal 15 (SIGTERM) is preferred, usually). In this case, the JVM can’t execute the
finallyblock, because the OS won’t let it run any longer.