Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the finally block execute if the thread running the function is interrupted?

Tags:

If I have a function with a try/finally section, and the thread running it is interrupted while in the try block, will the finally block execute before the interruption actually occurs?

like image 308
user940016 Avatar asked Jan 29 '13 06:01

user940016


People also ask

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 the finally block always execute?

A finally block always executes, regardless of whether an exception is thrown. The following code example uses a try / catch block to catch an ArgumentOutOfRangeException.

When finally block will not execute?

A finally block will not execute due to other conditions like when JVM runs out of memory when our java process is killed forcefully from task manager or console when our machine shuts down due to power failure and deadlock condition in our try block.

Does finally execute after break?

Break Statement with Finally Statement The break statement exits a loop in Python. If you do error handling in a loop and you break the loop, the code after break will not be executed.


2 Answers

According to the Java Tutorials, "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."

Here's the full passage:

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs. But finally is useful for more than just exception handling — it allows the programmer to avoid having cleanup code accidentally bypassed by a return, continue, or break. Putting cleanup code in a finally block is always a good practice, even when no exceptions are anticipated.

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.

class Thread1 implements Runnable {      @Override     public void run() {         try {             Thread.sleep(10000);         } catch (InterruptedException e) {             e.printStackTrace();         } finally {             System.out.println("finally executed");         }     } } 

...

t1.start(); t1.interrupt(); 

It prints - finally executed

like image 166
Subhrajyoti Majumder Avatar answered Nov 18 '22 20:11

Subhrajyoti Majumder


A Thread Interrupt in Java is just setting a flag. It doesn't cause anything special to happen to currently executing code, or affect the flow of control.

If your thread is engaged in, or attempts to enter, an operation that throws InterruptedException, then the exception is thrown from the point where that method is invoked and if it's inside a try block, the finally will execute before the exception leaves just like normal.

like image 31
Affe Avatar answered Nov 18 '22 19:11

Affe