请看以下代码
public static void main(String[] args) { Thread t1 = new Thread() { @Override public void run() { while (true) System.out.println(isInterrupted()); } }; t1.start(); t1.interrupt(); }
运行结果
说明调用了Thread.interrupt()后,Thread.isInterrupted就为true了
但是线程并不会立刻停止,只有遇到阻塞状态时,Interrupted状态的线程才会停止
我们也可以使用Interrupted作为线程是否继续执行的控制量,这样即使不出现阻塞情况,线程也会因为控制条件为false而停止
while (!isInterrupted()) System.out.println("hello");