Monday, 1 May 2017

10. Interruption


1. How a Thread can interrupt another Thread
1.       A Thread can interrupt a sleeping thread or waiting thread by using Interrupt() of Thread class
a.       Public   void  interrupt()
2.       Interrupt() will interrupt the thread which is in sleep, waiting state.
public class MyThread extends Thread {
  public void run() {
        for(int i=0; i<3; i++){
         try{
              System.out.println(“i am lazy thread”);
              Thread.sleep(2000);
         }catch(Exception e){
              System.out.println(“i got interrupted”);
         }
        }
  }
  public static void main(String[] args)throws Exception{
        MyThread thread = new MyThread();
        thread.start();
        thread.interrupt();
        System.out.println(“end of main thread”);
  }
}
O/P :
i am lazy thread
i got interrupted
end of main thread


3.       In above program, main thread interrupts child thread.
4.       Whenever we are calling interrupt(), if the target thread not in sleeping or waiting state then there is no impact of interrupt call immediately, but it not wasted the call.
5.       Interrupt call will be waited until target thread entered into sleeping or waiting state.
6.       If target thread entered into sleeping or waiting state then immediately interrupt call will interrupt target thread.
7.   Once sleeping or waiting thread is interrupted it will throw InterruptedException.
8.      If the target thread never entered into sleeping or waiting state in it life time then no impact of interrupt call, in this case only interrupt call will be wasted.
public class MyThread extends Thread {
  public void run() {
        for(int i=0; i<10000; i++){
              System.out.println(“i am lazy thread”);
        }
        try{
              Thread.sleep(2000);
         }catch(Exception e){
              System.out.println(“i got interrupted”);
         }
  }
  public static void main(String[] args)throws Exception{
        MyThread thread = new MyThread();
        thread.start();
        thread.interrupt();
        System.out.println(“end of main thread”);
  }
}
thread.interrupt();
Comment thread.interrupt();
i am lazy thread
i am lazy thread
i am lazy thread
i am lazy thread
.
.
.
.
i am lazy thread
i am lazy thread
i got interrupted
end of main thread
end of main thread
2. Why interrupt
  1. An interrupt is an indication to a thread that it should stop what it is doing and do something else.
  2. It's up to the programmer to decide exactly how a thread responds to an interrupt, but it is very common for the thread to terminate.
  3. A thread sends an interrupt by invoking interrupt() on the Thread object for the thread to be interrupted.
3. How many ways to interrupt a thread
  1. Once if we call interrupt() , than interrupt flag for that particular thread will be set.
  2. If a thread interrupt flag is true and it is in any one of the following methods , then thread will throw InterruptedException and clear the interrupt flag.
    1. wait(..)
    2. sleep(..)
    3. join(..)
  3. Interrupting a thread that is not alive need not have any effect.
  4. If the current thread cannot modify this thread then it will throw SecurityException (future classes detail explanation)
  5. If thread is blocked in I/O operations then the channel is closed , then thread interrupt flag will be set to true and throws ClosedByInterruptException
4. Can we check is interrupt flag for a particular thread is set or not.
  1. Yes, we can check by using isInterrupted()
    public      boolean      isInterrupted()
5. Can we clear the interrupt flag.
  1. Yes, One more method is there to check interrupt flag is set or not , if set it will clear the flag automatically.
    public      static   boolean    interrupted()
  1. Above method clear the interrupt flag and also returns state of interrupt flag before modifying.
  2. This method will get and clear the currently executing thread's interrupted flag.(it is static)
6. Can we throw InterrupException without thread in any waiting or blocking states based on interrupt flag.
  1. Yes, We can check interrupt flag is set or not, once it is set clear the flag and throw InterruptException.
if (Thread.interrupted()) {
   throw new InterruptedException();
}

7. interrupt()  vs   isInterrupt()    vs   interrupted()

interrupt()
isInterrupt()
interrupted()
Flag set
Sets the interrupt flag to true
It will not set
Set the interrupted flag to false
Syntax
Public   void  interrupt()
public      boolean      isInterrupted()
public      static   boolean    interrupted()
Calling
We can call on any thread object
We can call on any thread object
We can call on currently executing thread.
returns
void
Returns interrupt flag
Returns interrupted flag




1 comment:

  1. I have read your blog its very attractive and impressive,Thanks for sharing


    Java Online Course

    ReplyDelete

3. Java Program to create Binary Tree