Interrupting Threads Core Java

A thread terminates when its run method returns, by executing a return statement, after executing the last statement in the method body, or if an exception occurs that is not caught in the method. In the initial release of Java, there also was a stop method that another thread could call to terminate a thread. However, that method is now deprecated.

There is a way to force a thread to terminate. However, the interrupt method can be used to request termination of a thread. When the interrupt method is called on a thread, the interrupted status of the thread is set. This is a boolean flag that is present in every thread. Each thread should occasionally check whether it has been interrupted.

To find out whether the interrupted status was set, first call the static Thread .current Thread method to get the current thread and then call the is Interrupted method:

However, if a thread is blocked, it cannot check the interrupted status. This is where the InterruptedException comes in. When the interrupt method is called on a thread that blocks on a call such as sleep or wait, the blocking call is terminated by an InterruptedException. (There are blocking I/O calls that cannot be interrupted.)

There is no language requirement that a thread that is interrupted should terminate. Interrupting a thread simply grabs its attention. The interrupted thread can decide how to react to the interruption. Some threads are so important that they should handle the exception and continue. But quite commonly, a thread will simply want to interpret an interruption as a request for termination. The run method of such a thread has the following form:

The isInterrupted check is neither necessary nor useful if you call the sleep method (or another interruptible method) after every work iteration. If you call the sleepmethod when the interrupted status is set, it doesn’t sleep. Instead, it clears the status (!) and throws an InterruptedException. Therefore, if your loop calls sleep, don’t check the interrupted status. Instead, catch the InterruptedException, like this:

NOTE: There are two very similar methods, interrupted and isInterrupted. The interrupted method is a static method that checks whether the current thread has been interrupted. Further more,calling the interrupted method clears the interrupted status of the thread. On theother hand, the isInterrupted method is an instance method that you can use to check whether any thread has been interrupted. Calling it does not change the interrupted status.

You’ll find lots of published code in which the InterruptedException is squelched at a low level, like this:

Don’t do that! If you can’t think of anything good to do in the catch clause, you still have two reasonable choices:

  • In the catch clause, call Thread.currentThread().interrupt() to set the interrupted status.
Then the caller can test it.
  • Or, even better, tag your method with throws InterruptedException and drop the try block. Then the caller (or, ultimately, the run method) can catch it.

java.lang.Thread

  • void interrupt()
    sends an interrupt request to a thread. The interrupted status of the thread is set totrue. If the thread is currently blocked by a call to sleep, then an InterruptedExceptionis thrown.
  • static boolean interrupted()
    tests whether the current thread (that is, the thread that is executing this instruction) has been interrupted. Note that this is a static method. The call has a side effect—it resets the interrupted status of the current thread to false.
  • boolean isInterrupted()
    tests whether a thread has been interrupted. Unlike the static interrupted method, this call does not change the interrupted status of the thread.
  • static Thread currentThread()
    returns the Thread object representing the currently executing thread.


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Core Java Topics