Thread Interruption in Java

Thread interrupt is an indication to a thread that it should stop doing what it is currently doing and either terminate or do something else. What a thread does after being interrupted is up to you but the convention is that a thread should be terminated once interrupted.

How thread interruption in Java works

In Java Thread class there is a method interrupt() which interrupts the calling thread. Once the thread is interrupted its interrupt status is set to true and then based on whether the thread is currently blocked or not following activity takes place-

  1. If this thread is blocked in an invocation of the wait(), wait(long), or wait(long, int) methods of the Object class, or of the join(), join(long), join(long, int), sleep(long), or sleep(long, int), methods of this class, then its interrupt status will be cleared (set to false again) and it will receive an InterruptedException.
  2. If this thread is blocked in an I/O operation upon an InterruptibleChannel then the channel will be closed, the thread’s interrupt status will be set, and the thread will receive a ClosedByInterruptException.
  3. If this thread is blocked in a Selector then the thread’s interrupt status will be set and it will return immediately from the selection operation, possibly with a non-zero value, just as if the selector’s wakeup method were invoked.
  4. If a thread that is not blocked is interrupted, then the thread’s interrupt status will be set.

Methods related to thread interruption in Java Thread class

Apart from the method interrupt() already discussed above there are two more methods in java.lang.Thread class related to thread interruption interrupted() and isInterrupted().

  • void interrupt()– Interrupts this thread.
  • static boolean interrupted()– Checks whether the current thread has been interrupted. Also clears the interrupted status of the thread.
  • boolean isInterrupted()– Tests whether this thread has been interrupted. This method doesn’t change the interrupted status of the thread in any way.

Thread interruption examples

Thread interruption in Java is generally used to terminate a blocking thread. So you can choose to just return after interrupting a sleeping or waiting thread.

Output

In the thread’s run method there is a loop to print number from 1 to 5, after each number display, sleep method is called on the thread. Thread’s interrupt method is also called in the mean time which interrupts the blocked thread. Also you can see that the interrupt status is true after the interrupt() method is called and later the interrupt status is cleared.

Wrapping and rethrowing the InterruptedException

Rather than just returning as done in the previous example you may choose to wrap the InterruptedException and rethrow it.

Output

Interrupting a non-blocking thread

If you interrupt a thread that is not blocking because of invoking wait(), join() or sleep() methods, then just the interrupt status of the thread changes. Using the isInterrupted() method you can check the interrupt status of the thread and choose to execute some logic in that condition.

Output

As you can see in this case interrupt status remains true.

That’s all for the topic Thread Interruption in Java. If something is missing or you have something to share about the topic please write a comment.


You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.