interrupt():中断线程(设置中断标识位)
interrupted():静态方法 测试当前线程是否已经中断(当前线程指运行此方法的线程)
isInterrupted():测试线程是否已经中断
中断的理解:
通过中断并不能停止一个线程 需要被中断的线程自己去处理
被中断的线程如何知道自己被中断了?
线程对象有一个标识位来表示是否被中断了
测试:
package com.tony;
public class MyThread extends Thread {
@Override
public void run() {
System.out.println("running...");
}
}
public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(5*1000);
myThread.interrupt(); // 设置中断(中断位为true)
System.out.println("是否中断了interrupted():" + myThread.interrupted());
System.out.println("是否中断了interrupted():" + myThread.interrupted());
}
}
输出结果:
running...
是否中断了interrupted():false
是否中断了interrupted():false
MyTread实现类被设置了中断位 为什么输出都是false
interrupted()测试的是当前线程是否中断 main线程没有被中断 所以false
package com.tony;
public class MyThread extends Thread {
@Override
public void run() {
System.out.println("是否中断了interrupted():" + this.interrupted());
System.out.println("是否中断了interrupted():" + this.interrupted());
}
}
package com.tony;
public class ThreadTest {
public static void main(String[] args) {
MyThread myThread = new MyThread();
myThread.start();
myThread.interrupt(); // 设置中断(中断为true)
}
}
输出结果:
是否中断了interrupted():true
是否中断了interrupted():false
true:可以理解 当前线程是MyThread并被设置了中断位
false:???
查看API文档一目了然
第一次调用时 先判断中断位在清除这个中断位
没有中断位了 第二次调用时 判断是否已经中断返回false
package com.tony;
public class MyThread extends Thread {
@Override
public void run() {
System.out.println("running...");
}
}
package com.tony;
public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
myThread.start();
myThread.interrupt(); // 设置中断(中断为true)
System.out.println("是否中断了isInterrupted():" + myThread.isInterrupted());
System.out.println("是否中断了isInterrupted():" + myThread.isInterrupted());
}
}
输出结果:
是否中断了isInterrupted():true
是否中断了isInterrupted():true
running...
![](https://i-blog.csdnimg.cn/blog_migrate/8aabf253df69c967a4d93c77dc832dde.png)
使用例子:
package com.tony;
public class MyThread extends Thread {
@Override
public void run() {
for (int i = 0; i < 8888888; i++) {
if (this.isInterrupted()) {
System.out.println("线程被中断了");
return; // 线程退出
} else {
System.out.println("模拟线程正在干活...");
}
}
}
}
package com.tony;
public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(5 * 1000);
myThread.interrupt();
}
}
输出结果:
模拟线程正在干活...
模拟线程正在干活...
模拟线程正在干活...
......
模拟线程正在干活...
模拟线程正在干活...
模拟线程正在干活...
线程被中断了
![](https://i-blog.csdnimg.cn/blog_migrate/c2eb1d4a9021420b630a10fb2653c6fd.png)
测试在sleep中断线程:
package com.tony;
public class MyThread extends Thread {
@Override
public void run() {
try {
Thread.sleep(5*1000);
}catch(InterruptedException e) {
System.out.println("sleep中被中断:" + this.isInterrupted());
e.printStackTrace();
}
}
}
package com.tony;
public class ThreadTest {
public static void main(String[] args) throws InterruptedException {
MyThread myThread = new MyThread();
myThread.start();
Thread.sleep(2 * 1000);
myThread.interrupt();
}
}
输出结果:
sleep中被中断:false
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at com.tony.MyThread.run(MyThread.java:8)
false?
sleep中被中断 中断位被清除 所以返回false