Interrupt Thread interrupted () thread

problem:

  1, interrupt threads.

  2. Why interrupt blocking the thread, throws an exception.

Code Example:  

com.hdwl.netty Package; 

public class ThreadInterrupted { 

  public static void main (String [] args) { 
// testNoInterrupted (); 
// testInterrupted (); 
    testInterruptedWithBlock (); 
  } 

  // interrupt the test blocked thread 
  private static void testInterruptedWithBlock () { 
    MyInterruptedBlockThread MIBT new new MyInterruptedBlockThread = (); 
    mibt.start (); 
    the try { 
      the Thread.sleep (1000); 
    } the catch (InterruptedException E) { 
      e.printStackTrace (); 
    } 
    mibt.interrupt (); // set the interrupt mark 
  } 

  // interrupt thread non-blocking demonstration  
  private static void testInterrupted () {
    MyInterruptedNoBlockThread MIBT new new MyInterruptedNoBlockThread = (); 
    mibt.start ();
    {the try 
      the Thread.sleep (1000); 
    } the catch (InterruptedException E) { 
      e.printStackTrace (); 
    } 
    mibt.interrupt (); // Flag Set thread interrupts 
  } 

  // uninterrupted thread presentation, the interruption mark is not provided, All threads will never be interrupted. 
  static void testNoInterrupted Private () { 
    MyInterruptedNoBlockThread MIBT new new MyInterruptedNoBlockThread = (); 
    mibt.start (); 
  } 

} 
// blocked thread 
class MyInterruptedBlockThread the extends the Thread { 
  @Override  
  public void RUN () { 
    // If the thread is interrupted position true, then the end of the while loop 
    while (! isInterrupted ()) {
      System.out.println ( "running ......"); 
      the try { 
        SLEEP (2000); 
      } the catch (InterruptedException E) { 
        e.printStackTrace (); 
        // if the blocking thread is interrupted, marking the thread interrupt is reset, it is necessary to re-set 
        interrupt (); 
      } 
    } 
    System.out.println ( "end of the thread!"); 
  } 
} 
// non-blocking thread 
class MyInterruptedNoBlockThread the extends the thread { 
  @Override 
  public void RUN () { 
    // If the thread is interrupted position is true, the end of the while loop 
    while {(isInterrupted ()!) 
      System.out.println ( "running ......"); 
    } 
    System.out.println ( "end of the thread!"); 
  } 
}

Answer questions:

  1, the thread is interrupted, the use of set interrupt marked manner.

  2, interrupt blocking the thread throws an exception, in order not to interrupt the endless thread. Because the set interrupt mark, the thread does not stop immediately, will have to wait for the next arrival of CPU time slice, the thread can be terminated by return interrupted the method. If a blocked thread B, dependent on the thread A notify (or other operations, in order to wake up the thread B), then thread A, B and set the interrupt label, then thread A has died, but also to wait until thread B wake up, it can be the basis interrupted method, thread interrupt operation. Therefore, interrupt blocking the thread, you must throw an exception, to facilitate our subsequent processing.

   

Guess you like

Origin www.cnblogs.com/chen--biao/p/11358405.html