Java – if the thread running the function is interrupted, does the finally block execute?
•
Java
If I have a function with a try / finally part and the thread running it is interrupted in the try block, does the finally block execute before the interrupt actually occurs?
Solution
According to the Java tutorials, "if the thread executing the try or catch code is interrupted or killed, the finally block may not be executed even if the whole application continues as a whole
This is a complete paragraph:
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 – last executed
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码