what course of action should be followed when a thread has been interrupted (due to an exception)?

by Holden Schuster 6 min read

Thread provides the interrupt () method for interrupting a thread, and to query whether a thread has been interrupted, we can use the isInterrupted () method. Occasionally, we may wish to test whether the current thread has been interrupted and if so, to immediately throw this exception. Here, we can use the interrupted () method:

Full Answer

What is the interrupted thread exception?

The exception that is thrown when a Thread is interrupted while it is in a waiting state. The following code example shows the behavior of a running thread when it is interrupted and subsequently gets blocked.

Can the interrupt () method affect a thread that is blocked while waiting?

One area of confusion that surrounds the interrupt () method is in the area of I/O: can the interrupt () method affect a thread that is blocked while waiting for I/O? The answer for the time being is that it cannot, and you should not rely on its ability to do so. This may change in future releases of the virtual machine.

How does the target thread know when a thread has been interrupted?

The target thread knows it has been interrupted if it is executing a method that will throw an InterruptedException. Otherwise, the target thread must use one of these methods to check if it has been interrupted: Returns a boolean that indicates whether the current thread has been interrupted.

Why does the read () method throw an IOException when blocking a thread?

However, as it turns out, there are some implementations of the virtual machine-most notably, the Solaris native-thread implementation-that cause the interrupt () method to interrupt any pending I/O. Hence, if a thread that is blocked on the read () method is the target of the interrupt () method, the read () method will throw an IOException.

What is wait method?

The wait () method—like the sleep () and join () methods that we examined in Section 2.1 —may under certain circumstances throw an InterruptedException. These methods all throw such an exception when the thread in which they are executing is interrupted, which occurs when another thread calls this method:

What is an interrupt flag?

[ 4] The interrupt () method is a method of the Thread class, and it is used by one thread to signal another thread: it is possible (although it doesn’t really make sense) for a thread to interrupt itself.

Can interrupt affect threads waiting for I/O?

One area of confusion that surrounds the interrupt () method is in the area of I/O: can the interrupt () method affect a thread that is blocked while waiting for I/O? The answer for the time being is that it cannot, and you should not rely on its ability to do so. This may change in future releases of the virtual machine.

Does Java 1.0.2 work?

In Java 1.0.2—and browsers such as Netscape 3.0 and Internet Explorer 3.0 that are based on that release—these methods do not work properly. In particular, the interrupt () method is not able to interrupt a thread that is sleeping.

Does thread interrupt work in Java?

Thread interruption works in Java 1.1 and later releases, but it does not work in many 1.1-based browsers. For now, we recommend that you use these methods only in Java applications or in applets run with the Java plug-in. The effect of the interrupt () method depends on whether the target of the interruption is executing a method ...

image