Interrupting Threads


A thread terminates when its run method returns. In JDK 1.0, there also was a stop method that another thread could call to terminate a thread. However, that method is now deprecated. We discuss the reason on page 46.

There is no longer 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.currentThread method to get the current thread and then call the isInterrupted method:


while (!Thread.currentThread().isInterrupted() && more work to do)
{
   do more work
}

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 blocked thread, the blocking call (such as sleep or wait) is terminated by an InterruptedException.

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:


public void run()
{
    try
    {
       . . .
       while (!Thread.currentThread().isInterrupted() && more work to do)
       {
          do more work
       }
    }
    catch(InterruptedException e)
    {
       // thread was interrupted during sleep or wait
    }
    finally
    {
       cleanup, if required
    }
    // exiting the run method terminates the thread
 }

The isInterrupted check is not necessary if you call the sleep method after every work iteration. The sleep method throws an InterruptedException if you call it when the interrupted status is set. Therefore, if your loop calls sleep, don't bother checking the interrupted status and simply catch the InterruptedException. Then your run method has the form


public void run()
{
   try
   {
      . . .
      while (more work to do)
      {
         do more work
         Thread.sleep(delay);
      }
   }
   catch(InterruptedException e)
   {
      // tHRead was interrupted during sleep or wait
   }
   finally
   {
      cleanup, if required
   }
   // exiting the run method terminates the thread
}

CAUTION

When the sleep method throws an InterruptedException, it also clears the interrupted status.


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. Furthermore, calling the interrupted method clears the interrupted status of the thread. On the other 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:

 void mySubTask() {    . . .    try { sleep(delay); }    catch (InterruptedException e) {} // DON'T IGNORE!    . . . } 

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.

     void mySubTask() {    . . .    try { sleep(delay); }    catch (InterruptedException e) { Thread().currentThread().interrupt(); }    . . . } 

  • 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.

     void mySubTask() throws InterruptedException {    . . .    sleep(delay);    . . . } 


 java.lang.Thread 1.0 

  • void interrupt()

    sends an interrupt request to a thread. The interrupted status of the thread is set to TRue. If the thread is currently blocked by a call to sleep, then an InterruptedException is 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 effectit 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.



    Core JavaT 2 Volume II - Advanced Features
    Building an On Demand Computing Environment with IBM: How to Optimize Your Current Infrastructure for Today and Tomorrow (MaxFacts Guidebook series)
    ISBN: 193164411X
    EAN: 2147483647
    Year: 2003
    Pages: 156
    Authors: Jim Hoskins

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net