Friday 5 July 2019

Interrupting a Thread

How a Thread can interrupt another thread?
  • If a Thread can interrupt a sleeping or waiting Thread by using interrupt()(break off) method of Thread class.
  • public void interrupt();
Example:

class MyThread extends Thread
{
              public void run()
              {
                             try
                             {
                                           for(int i=0;i<5;i++)
                                           {                                         
                                                          System.out.println("i am lazy Thread :"+i);
                                                          Thread.sleep(2000);
                                           }
                             }
                             catch (InterruptedException e)
                             {
                                           System.out.println("i got interrupted");
                             }
              }
}
class ThreadInterruptDemo
{
              public static void main(String[] args)
              {
                             MyThread t=new MyThread(); t.start();
                             //t.interrupt();  //--->1
                             System.out.println("end of main thread");
              }
}

  • If we are commenting line 1 then main Thread won't interrupt child Thread and hence child Thread will be continued until its completion.
  • If we are not commenting line 1 then main Thread interrupts child Thread and hence child Thread won't continued until its completion in this case the output is:
                     End of main thread
                     I am lazy Thread: 0
                     I got interrupted
Note:
  • Whenever we are calling interrupt() method we may not see the effect immediately, if the target Thread is in sleeping or waiting state it will be interrupted immediately.
  • If the target Thread is not in sleeping or waiting state then interrupt call will wait until target Thread will enter into sleeping or waiting state. Once target Thread entered into sleeping or waiting state it will effect immediately.
  • In its lifetime if the target Thread never entered into sleeping or waiting state then there is no impact of interrupt call simply interrupt call will be wasted.
Example:


class MyThread extends Thread
{
              public void run()
              {
                             for(int i=0;i<5;i++)
                             {
                                           System.out.println("iam lazy thread");
                             }
                             System.out.println("I'm entered into sleeping stage");
                             try
                             {
                                           Thread.sleep(3000);
                             }
                             catch (InterruptedException e)
                             {
                                           System.out.println("i got interrupted");
                             }
              }
}

class ThreadInterruptDemo1
{
              public static void main(String[] args)
              {
                             MyThread t=new MyThread();
                             t.start();
                             t.interrupt();
                             System.out.println("end of main thread");
              }
}
 
  • In the above program interrupt() method call invoked by main Thread will wait until child Thread entered into sleeping state.
  • Once child Thread entered into sleeping state then it will be interrupted immediately.

Thanks..!!

No comments:

Post a Comment