Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can an interrupted Java thread really skip a finally clause?

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?

like image 414
SusanW Avatar asked Jun 05 '17 19:06

SusanW


People also ask

What happens when a thread gets interrupted java?

In Java, an InterruptedException will be thrown if the thread is currently blocking. If the thread is not blocking, the exception will not be thrown. For . NET languages, a ThreadInterruptedException will be thrown if the thread is currently blocking.

What happens when a thread gets interrupted?

The "interrupted" status of the thread is set to true. If the thread is currently blocked by a call to sleep or wait, an InterruptedException is thrown. tests whether or not the current thread (that is, the thread that is executing this instruction) has been interrupted.

Does finally always executed java?

Yes, the finally block is always get executed unless there is an abnormal program termination either resulting from a JVM crash or from a call to System. exit(). A finally block is always get executed whether the exception has occurred or not.

How do I know if a thread is interrupted?

isInterrupted() The interrupted() is a static method in Thread class that determines if the current thread has been interrupted. The isInterrupted() is an instance method that tests if this thread instance has been interrupted. "The interrupted status of the thread is cleared by this method".

How to interrupt a thread in Java?

Example of interrupting thread that behaves normally. If thread is not in sleeping or waiting state, calling the interrupt() method sets the interrupted flag to true that can be used to stop the thread by the java programmer later.

What is an InterruptedException in Java?

An InterruptedException is thrown when a thread is interrupted while it's waiting, sleeping, or otherwise occupied. In other words, some code has called the interrupt () method on our thread. It's a checked exception, and many blocking operations in Java can throw it. 3.1.

What is the difference between isinterrupted and interrupt in Java?

If thread is not in sleeping or waiting state, calling the interrupt () method sets the interrupted flag to true that can be used to stop the thread by the java programmer later. TestInterruptingThread3.java What about isInterrupted and interrupted method? The isInterrupted () method returns the interrupted flag either true or false.

What is an interrupt in Java concurrency?

As per the tutorial on concurrency in Java Documentation An interrupt is an indication to a thread that it should stop what it is doing and do something else. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate


1 Answers

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.

like image 113
Nathan Hughes Avatar answered Oct 23 '22 06:10

Nathan Hughes