Thread中的interrupt(),interrupted(),isInterrupted()方法的详解
2016-12-18 20:44
609 查看
1.interrupt()
使用来中断线程作用的。除非当前线程处于中断状态,这个方法总是可以被执行的。如果当前线程被
结果
2.interrupted
测试当前线程是否中断,这个方法将会清除中断状态,换句话说,如果这个方法被成功调用2次,第二次将会返回false,除非当前线程再次被中断,可以在第一个调用后,第二次调用前去检测它。
结果
3.isInterrupted()
返回线程中断状态,调用isInterrupted()方法不会影响中断状态。
结果
使用来中断线程作用的。除非当前线程处于中断状态,这个方法总是可以被执行的。如果当前线程被
Object#wait() wait(long)或者
Thread#join() join(long),sleep()方法所阻塞时,然而中断状态将被清空变成
false,如果被IO/NIO阻塞,使用该方法,中断状态将被设置成
true,未中断状态为
false,中断状态为
true
public class InterruptTest { public static void main(String[] args) { System.out.println(Thread.currentThread().getName()+" interrupt before "+Thread.currentThread().isInterrupted()); Thread.currentThread().interrupt(); System.out.println(Thread.currentThread().getName()+" interrupt after "+Thread.currentThread().isInterrupted()); } } 结果: main interrupt before false main interrupt after true class TestThread extends Thread { public TestThread(String name){ setName(name); } public void run() { try{ Thread.sleep(10); }catch(Exception e){ e.printStackTrace(); } } } public class InterruptTest { public static void main(String[] args) throws InterruptedException { TestThread tt = new TestThread("test"); tt.start(); System.out.println(tt.getName()+" interrupt before "+tt.isInterrupted()); tt.interrupt();//中断sleep将会清空中断状态 Thread.sleep(1000);//等待test线程 System.out.println(tt.getName()+" interrupt after "+tt.isInterrupted()); } }
结果
test interrupt before false java.lang.InterruptedException: sleep interrupted at java.lang.Thread.sleep(Native Method) at thread2.TestThread.run(InterruptTest.java:11) test interrupt after false
2.interrupted
测试当前线程是否中断,这个方法将会清除中断状态,换句话说,如果这个方法被成功调用2次,第二次将会返回false,除非当前线程再次被中断,可以在第一个调用后,第二次调用前去检测它。
public class InterruptTest { public static void main(String[] args) throws InterruptedException { System.out.println(Thread.currentThread().getName()+" interrupt before "+Thread.currentThread().interrupted()); Thread.currentThread().interrupt(); System.out.println(Thread.currentThread().getName()+" interrupt after once "+Thread.currentThread().interrupted());//调用之后清除中断状态 System.out.println(Thread.currentThread().getName()+" interrupt after twice "+Thread.currentThread().interrupted()); } }
结果
main interrupt before false main interrupt after once true main interrupt after twice false
3.isInterrupted()
返回线程中断状态,调用isInterrupted()方法不会影响中断状态。
public class InterruptTest { public static void main(String[] args) throws InterruptedException { System.out.println(Thread.currentThread().getName()+" interrupt before "+Thread.currentThread().isInterrupted()); Thread.currentThread().interrupt(); System.out.println(Thread.currentThread().getName()+" interrupt after once "+Thread.currentThread().isInterrupted()); System.out.println(Thread.currentThread().getName()+" interrupt after twice "+Thread.currentThread().isInterrupted()); System.out.println(Thread.currentThread().getName()+" interrupt after three "+Thread.currentThread().isInterrupted()); } }
结果
main interrupt before false main interrupt after once true main interrupt after twice true main interrupt after three true
相关文章推荐
- Thread类的interrupt,interrupted,isInterrupted方法的理解
- Thread类的interrupt,interrupted,isInterrupted方法的理解
- interrupt、interrupted 、isInterrupted 详解
- Java Thread的interrupt方法详解
- Thread类的interrupt(),interrupted(),isInterrupted()
- Java Thread的中断以及interrupt()、isInterrupted()、interrupted
- interrupt(), isInterrupted(), interrupted() 方法的区别
- Thread的 interrupt,interrupted , iSinterrupted ,isAlive,join 用法
- Thread类的interrupt,interrupted,isInterrupted方法的理解
- Java Thread的interrupted()、isInterrupted()、interrupt()三者的区别
- 【多线程】interrupt、interrupted、isInterrupted方法
- Thread的interrupt、isInterrupted、interrupted源码探索
- Java并发08:Thread的基本方法(5)-interrupt()、isInterrupted()
- [线程的interrupt()方法,interrupted()和isInterrupted()] 的区别
- Thread类的interrupt,interrupted,isInterrupted方法的理解
- Thread.interrupt()方法
- 使用interrupt()方法和isInterrupted()方法更好的实现主线程和子线程的配合
- Thread.interrupt()方法很详细的介绍
- t.interrupt(), Thread.interrupted(), native isInterrupted()
- 使用interrupt()方法和isInterrupted()方法实现了线程