16

I was checking into the oft repeated rumour that daemon threads on the JVM treat finally blocks in some special way (they don't, ok?), when I read this, from the Oracle Java tutorial:

Note: If the JVM exits while the try or catch code is being executed, then the finally block may not execute. Likewise, if the thread executing the try or catch code is interrupted or killed, the finally block may not execute even though the application as a whole continues.

(Emphasis is mine.) And the bit about interrupted caught my eye!

My belief is that if a thread is in try/catch code and is interrupted, then either we're in (or eventually enter) a state (eg sleep, wait) where we end up throwing an InterruptedException, or we aren't and we drop out normally or abnormally, but in all cases we will hit the finally clause.

What have I missed? Is there really a way for a Thread to be interrupted and then skip a finally, whilst the application continues?

SusanW
  • 1,550
  • 1
  • 12
  • 22
  • 4
    No it cant. The tutorial is sloppily worded. See https://stackoverflow.com/q/14576765/217324 – Nathan Hughes Jun 05 '17 at 19:15
  • When the last non-daemon thread exists, the process exits, and remaining daemon threads are abruptly terminated in whatever execution state they are in. Locks, try-finally, blocking IO, whatever, have no special relevance here. – Brian Goetz Jun 05 '17 at 19:27
  • @BrianGoetz And that's the same for non-daemons too under `System.exit()` conditions, right? – SusanW Jun 05 '17 at 19:30
  • Depends on whether this is a _normal_ or _abrupt_ shutdown. See https://stackoverflow.com/questions/19639319/java-shutdown-hook. – Brian Goetz Jun 05 '17 at 19:31
  • @BrianGoetz Yep, makes sense. Good book btw. :-) – SusanW Jun 05 '17 at 19:34

1 Answers1

11

Since you specifically ask about daemon threads: remember daemon threads go away when the jvm's last nondaemon thread terminates, so in that case a daemon thread can die without having executed finally blocks (or whatever other code it was about to execute, there's nothing special about finally blocks). Don't use a daemon thread for anything that you don't mind being dropped on the floor when the jvm terminates.

Otherwise, interruption doesn't do anything to shortcut finally blocks. The point of interruption is that the interrupted thread is in control and can finish its execution, including closing resources, however it needs to.

The tutorials can be very helpful but they are not authoritative. The wording in this case uses the word interrupted, it seems reasonable in this context to assume it is referring specifically to thread interruption, which is incorrect.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Ah, actually, I wasn't really meaning specifically about daemons - that was just for context. Hmm, misleading, oops. I'll leave it though, your answer is rather clarifying! (Can you add your point about tutorial sloppiness? That's really what i was looking for!) – SusanW Jun 05 '17 at 19:27
  • 2
    The wording isn't sloppy or confusing. It's wrong. What is sloppy and confusing here is saying it's sloppy and confusing. – user207421 Jun 05 '17 at 19:50
  • @EJP: agreed, reworded. – Nathan Hughes Jun 05 '17 at 20:08