在没有join sleep wait 方法时 Thread.interrupted()中断,若是true,则会清除(置为false 但是如果是false则不变),后面的访问是false
中断.isInterrupted()不改变中断状态
public class InterruptDemo {
private static class Mythread implements Runnable{
@Override
public void run(){
int i=0;
for(i=0;i<5;i++){
// System.out.println(Thread.interrupted());
System.out.println(Thread.currentThread().isInterrupted());
}
}
}
public static void main(String[] args) {
Mythread mythread =new Mythread();
Thread t1=new Thread(mythread);
t1.start();
t1.interrupt();
}
}
//输出
true
true
true
true
true
interrupted()会将原状态清除
public class InterruptDemo {
private static class Mythread implements Runnable{
@Override
public void run(){
int i=0;
for(i=0;i<5;i++){
System.out.println(Thread.interrupted());
// System.out.println(Thread.currentThread().isInterrupted());
}
}
}
public static void main(String[] args) {
Mythread mythread =new Mythread();
Thread t1=new Thread(mythread);
t1.start();
t1.interrupt();
}
}
//输出
true
false
false
false
false
如果有join sleep wait 方法时阻塞时 ,收到中断通知Thread.interrupted()会抛出一个异常interruptedException,当抛出异常后,当前线程的中断会被清除。