table of Contents:
- The concept of threads interrupted
- Methods interrupted
- to sum up
The concept interrupted thread:
Interrupt thread is a cooperative mechanism , not its execution thread to really interrupt, but notice the thread to interrupt.
Like you have a little sister to add a friend, and then the system sent a message to you, without adding or not depends on your own thing φ (> ω <*).
Methods interrupted:
1、public static boolean interrupted();
Thread determine whether the interrupt state in the interrupt status -true, non-disruptive state -false; if the thread is interrupted is cleared by identity .
. 1 public class InterruptTest { 2 public static void main (String [] args) { . 3 // whether the thread is interrupted . 4 System.out.println ( ". 1:" + Thread.interrupted ()); . 5 // disposed thread interrupts 6 Thread.currentThread () interrupt ();. 7 // if the thread is interrupted 8 System.out.println ( "2:" + Thread.interrupted ()); 9 // if the thread has been interrupted 10 System.out.println ( ". 3:" + Thread.interrupted ()); . 11 } 12 is }
operation result:
1: false
2: true
3: false
Line 4 of code, then the result is false thread running; current code line 6, the main thread interrupts, line 8 result true; immediately interrupted () method to clear the interrupt flag, so the result is false, line 10 .
2、public boolean isInterrupted();
Thread determine whether the interrupt state in the interrupt status -true, non-disruptive state -false
. 1 public class IsInterruptedTest { 2 public static void main (String [] args) { . 3 // current thread . 4 the Thread Thread = Thread.currentThread (); . 5 // whether the current thread is interrupted . 6 System.out.println ( ". 1: "+ thread.isInterrupted ()); . 7 // set the thread interruption identification . 8 Thread.interrupt (); . 9 // determining whether the interrupted threads 10 System.out.println (" 2: "+ thread.isInterrupted ()); 11 // determine whether the thread has been interrupted 12 System.out.println("3: " + thread.isInterrupted()); 13 try { 14 // 线程休眠 15 Thread.sleep(2000); 16 System.out.println("not interrupted..."); 17 } catch (Exception e) { 18 System.out.println("Thread.sleep interrupted..."); 19 // 判断线程是否被中断 20 System.out.println("4: " + thread.isInterrupted()); 21 } 22 System.out.println("5: " + thread.isInterrupted()); 23 } 24 }
operation result:
1: false
2: true
3: true
Thread.sleep interrupted...
4: false
5: false
The first 12 lines and interrupted the same way , but will not clear the interrupt flag , so the results were false, true, true; because the thread has been interrupted, so the implementation of the second row 15 Thread.sleep will throw InterruptedException (2000) exception, Therefore, print Thread.sleep interrupted ...; after executing the interrupt status clear, so the result is a line of 18, 22 false.
3、public void interrupt();
Thread interrupt, and interrupt flag is set to true.
to sum up:
1, interrupt () method is the only possible to interrupt state is set to true process.
2, interrupted () method will be the current thread's interrupt status clear .