直接贴上例子
public class InteruptTest extends Thread {
static int i = 0;
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
// i happy run , please break me
System.out.println("I"m runing " + i++);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();//①:发出中止要求,设置中止状况
System.out.println(Thread.currentThread().isInterrupted());//②:断定中止状况(不消除中止状况)
System.out.println(Thread.interrupted());//③:断定中止状况(消除中止状况)
}
System.out.println("current thread haven"t been broken");
}
}
public static void main(String[] args) throws InterruptedException {
Thread t1 = new InteruptTest();
t1.start();
Thread.sleep(1000);
t1.interrupt();
}
}public class InteruptTest extends Thread