1. Interrupt是指对该线程设置了终止状态, 并没有终止该线程。
2. Interrupted是指判断当前线程是否终止了, 并且会由于interrupt设置的线程终止状态。
public class MyThread extends Thread {
private int count;
public void run() {
for (int i = 0; i < 100000; i++) {
count ++;
}
}
}
public class TestMain {
public static void main(String[] args) throws InterruptedException {
MyThread mt = new MyThread();
mt.start();
mt.interrupt();
System.out.println(mt.interrupted()); // 当前线程是否中断状态 false
System.out.println(mt.interrupted());// 当前线程是否中断状态 false
}
}
public class TestInterrupted {
public static void main(String[] args) {
Thread.currentThread().interrupt(); // 此时设置为中断状态
System.out.println(Thread.interrupted()); // 此时中断状态为True 但是 此方法清除了中断状态
System.out.println(Thread.interrupted()); // 此时中断状态为False
}
}
3. IsInterruped是指判断线程Thread对象是否已经是终止状态,但并不清除状态。
public class IsInterrupted {
public static void main(String[] args) {
MyThread mt = new MyThread();
mt.start();
mt.interrupt();
System.out.println(mt.isInterrupted()); // 判断测试线程是否处于中断状态 并且不清除状态标识
System.out.println(mt.isInterrupted()); // 判断测试线程Thread对象是否已经是中断状态 但不清除状态标识.
}
}
没懂的朋友联系我 wangyan199366 (wechat)