java并发:中断一个正在运行的线程

时间:2023-03-09 15:47:07
java并发:中断一个正在运行的线程

要使任务和线程能安全可靠地停止,并不是一件很容易的事情,java没有提供任何机制来安全地终止线程,那么我们该怎么办呢?

下面我们先来了解一下java中的中断机制:

java并发:中断一个正在运行的线程

java中断机制是一种协作机制,也就是说在某个线程中对另一个线程调用interrupt()方法并不能直接终止该线程,需要被中断的线程自己处理中断请求

interrupted() 和 isInterrupted()的主要区别:

非静态方法isInterrupted()用来查询某线程的中断状态,且不会改变该线程的中断状态标识;

静态方法interrupted()检查中断状态时,中断状态会被清零(置为false)。

在下面的例子中,主线程通过调用t.interrupt()方法将线程t的中断状态设置为true,线程t可以在合适的时候调用interrupted()方法或isInterrupted()方法来检测其中断状态并做相应处理:

package com.test;

public class ThreadTest extends Thread {
public void run() {
while (true) {
if (Thread.interrupted()) {
System.out.println("Someone interrupted me.");
break;
} else {
System.out.println("Going...");
}
long now = System.currentTimeMillis();
while (System.currentTimeMillis() - now < 1000) {
//让循环持续一段时间,打印的输出语句会少一些
}
}
} public static void main(String[] args) throws InterruptedException {
ThreadTest t = new ThreadTest();
t.start();
Thread.sleep(3000);
t.interrupt();
}
}

结果如下:

Going...
Going...
Going...
Someone interrupted me.

(1)对于非阻塞中的线程, 只是改变了中断状态, 即Thread.isInterrupted()将返回true;

(2)对于可取消的阻塞状态中的线程, 比如等待在Thread.sleep(), Object.wait(), Thread.join(),这些函数上的线程,  这个线程收到中断信号后, 会抛出InterruptedException, 同时会把中断状态置回为false.

下面这段程序会使得阻塞在Thread.join()方法上的主线程在收到中断信号后结束:

package com.test;

public class ThreadTest extends Thread { 

    private Thread parent;  

    public ThreadTest(Thread parent){
this.parent = parent;
} public void run() {
while (true) {
System.out.println("sub thread is running...");
long now = System.currentTimeMillis();
while (System.currentTimeMillis() - now < 2000) {
//让循环持续一段时间,打印的输出语句会少一些
}
parent.interrupt();
}
} public static void main(String[] args){
ThreadTest t = new ThreadTest(Thread.currentThread());
t.start();
try {
t.join();
} catch (InterruptedException e) {
System.out.println("Parent thread will die...");
}
}
}

结果如下:

sub thread is running...
Parent thread will die...
sub thread is running...
sub thread is running...
sub thread is running...
sub thread is running...

从结果可以看出,线程t发出中断请求后主线程结束(此时线程t仍在运行)