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

后端 未结 6 1142
执笔经年
执笔经年 2020-12-17 09:01

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 actu

6条回答
  •  眼角桃花
    2020-12-17 09:48

    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.

提交回复
热议问题