interrupt() method of thread in java


Contents of page :
  • interrupt() method >
  • Program to terminate infinitely running thread using interrupt() method >
  • Important point about interrupt() method >

interrupt() method >
When thread is interrupted, if it in waiting state caused by invocation of wait(), sleep or join()  method then its interrupt status will be cleared and it will receive an InterruptedException.
Thread can only only interrupt themselves, if any other thread tries to interrupt thread than it may cause SecurityException to be thrown.


Logic behind the program is that main Thread will call newThread and newThread will keep on running continuously/infinitely until not being interrupted, but mainThread will be running intermittently and be waiting for user to press enter. As soon as user will press enter main Thread will call interrupt() method on newThread and in run() method we will catch that exception to stop continuously/infinitely running newThread.

Program to terminate infinitely running thread using interrupt() method >
class MyRunnable implements Runnable {
   public void run() {
          int i = 0;
          try {
                 while (!Thread.currentThread().isInterrupted()) {
                       Thread.sleep(1000);
                       System.out.println(i++ + " Please press enter key to stop "
                                     + Thread.currentThread().getName());
                 }
          } catch (InterruptedException e) {
                 System.out.println(Thread.currentThread().getName() + " ENDED");
          }
   }
}
/** Copyright (c), AnkitMittal JavaMadeSoEasy.com */
public class TerminatingThreadUsingInterrupt {
   public static void main(String args[]) throws Exception {
          MyRunnable obj = new MyRunnable();
          Thread t = new Thread(obj, "Thread-1");
          t.start();
          System.out.println(Thread.currentThread().getName()
                       + " thread waiting for user to press enter");
          System.in.read();
          t.interrupt();
          System.out.println(Thread.currentThread().getName() + " thread ENDED");
   }
}
/* OUTPUT
main thread waiting for user to press enter
0 Please press enter key to stop Thread-1
1 Please press enter key to stop Thread-1
2 Please press enter key to stop Thread-1
3 Please press enter key to stop Thread-1
main thread ENDED
Thread-1 ENDED
*/

Important point about interrupt() method >
Interrupting basically sends a message to the thread  indicating it has been interrupted but it doesn't cause  a thread to stop immediately, if sleep is called, thread immediately throws  InterruptedException.



RELATED LINKS>



Important Thread methods (salient features, usage with programs)>

Sleep() method in threads - 10 key features with programs

Wait() and notify() methods- Definition, 8 key features, solving consumer producer problem with & without these methods and consequences of not using wait() and notify() methods.

Daemon threads - 12 salient features of Daemon Thread

2 alternate ways to stop thread, as stop() method is deprecated

Join() method - 10 salient features of join

Thread isAlive() method

interrupt() method of thread in java

holdsLock(object) method to find whether current thread holds the lock on monitor of specified object.

Thread priorities - setPriority() and getPriority() methods

Using Suspend and resume method in threads



Methods for Adding ShutdownHook and handling uncaught runtime exception >

Threads addShutdownHook method in java

Handling uncaught runtime exception generated in run method using - setDefaultUncaughtExceptionHandler method




eEdit
Must read for you :