interrupt

interrupt方法用于中断线程。调用该方法的线程的状态为将被置为"中断"状态。
注意:线程中断仅仅是置线程的中断状态位,不会停止线程。需要用户自己去监视线程的状态为并做处理(sleep,join,wait)。支持线程中断的方法(也就是线程中断后会抛出interruptedException的方法)就是在监视线程的中断状态,一旦线程的中断状态被置为“中断状态”,就会抛出中断异常。

 

调用线程的interrupt方法,并不能真正中断线程,只是给线程做了中断状态的标志
Thread.interrupted():测试当前线程是否处于中断状态。执行后将中断状态标志为false
Thread.isInterrupted(): 测试线程Thread对象是否已经处于中断状态。但不具有清除功能

Thread.currentThread().interrupt();
System.out.println(Thread.interrupted()); // true
System.out.println(Thread.interrupted()); // false
System.out.println(Thread.currentThread().isInterrupted()); // false 

 

InterruptedException - if any thread has interrupted the current thread. The interrupted status of the current thread is cleared when this exception is thrown.  

posted on 2018-11-01 16:46  xiaowater  阅读(240)  评论(0)    收藏  举报

导航