Java 没有提供一种安全,直接的方式来停止某个线程,而是提供了中断机制。中断机制是一种协作机制,也就是说通常中断并不能直接终止另一个线程,而需要中断的线程处理。 被中断的线程拥有完全的自主权,它既可以选择立即停止,也可以选择一段时候后停止,也可以选择不停止。
Thread 中可实现线程中断的方法
-
: 将线程的中断标志位设置为 true , 不会停止线程。interrupt()
-
:判断当前线程的中断标志位是否为 true , 不会清除中断标志位。isInterrupted()
-
: 判断当前线程的中断标志是否为true, 清除中断标志位,重置为 false。Thread.interrupted()
一个简单的中断使用的示例代码:
public class ThreadInterruptTest {
static int i = 0;
public static void main(String[] args) {
System.out.println("begin");
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
i++;
System.out.println(i);
//Thread.interrupted() 清除中断标志位
//Thread.currentThread().isInterrupted() 不会清除中断标志位
if (Thread.currentThread().isInterrupted()) {
System.out.println("=========");
}
if (i == 10) {
break;
}
}
}
});
t1.start();
//不会停止线程t1,只会设置一个中断标志位 flag=true
t1.interrupt();
}
}
利用中断机制停止线程
举个例子
while (!Thread.currentThread().isInterrupted() && more work to do) {
do more work
通过中断来停止正在执行的线程:
public class StopThread implements Runnable {
@Override
public void run() {
int count = 0;
while (!Thread.currentThread().isInterrupted() && count < 1000) {
System.out.println("count = " + count++);
}
System.out.println("线程停止: stop thread");
}
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(new StopThread());
thread.start();
Thread.sleep(5);
thread.interrupt();
}
}
输出结果:
注意:使用中断机制时一定要注意是否存在中断标志位被清除的情况
sleep 和 wait 和线程中断
我们修改一下上面的 run 方法:
@Override
public void run() {
int count = 0;
while (!Thread.currentThread().isInterrupted() && count < 1000) {
System.out.println("count = " + count++);
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("线程停止: stop thread");
}
打印结果如下:
在执行到 count == 99 的时候
总结
- sleep() 可以被中断 并且抛出中断异常:sleep interrupted, 清除中断标志位。
- wait() 可以被中断, 并且抛出中断异常:InterruptedException, 清除中断标志位。
- 如果要在
和sleep()
方法中断有效,需要捕获异常重新设置中断位。wait()