0
点赞
收藏
分享

微信扫一扫

Java中的线程中断

Java中的线程中断

1 线程中断相关方法介绍

Java多线程编程中的​​interrupt()​​方法、​​isInterrupted()​​方法和​​interrupted()​​方法都是跟线程中断相关的方法,都非常重要。这三个方法名称非常相似,不理解原理时容易混淆,这里分别介绍下,以加以区分。由于​​interrupt()​​方法和​​isInterrupted()​​方法都是实例方法(非类上的静态方法),因此我在前面加了个​​thread1​​,表示一个实例化的具体线程:

thread1.interrupt()方法

​thread1.interrupt()​​方法用来中断线程,所谓的中断,大家可以通俗的理解为​​打断​​。比如有两个线程​​a​​和​​b​​,当线程​​a​​因为某些原因想打断线程​​b​​时,​​a​​线程内部可以调用​​b.interrupt()​​。不过要注意,实现上是通过设置线程​​b​​的​​中断状态标记​​实现的。​​b​​线程代码运行期间,可以在一个循环体内不断的判断该​​中断状态标记​​,以确认是否真正响应​​a​​的中断请求(比如退出执行等等)。

thread1.isInterrupted()方法

​thread1.isInterrupted()​​方法用来获取一个线程的中断状态。比如有两个线程​​a​​和​​b​​,当线程​​a​​因为某些原因想打断线程​​b​​时,可以通过​​b.interrupt()​​对​​b​​进行中断。在线程​​b​​内部,可以判断自己的中断状态,是否是被中断的,然后根据中断状态确认是否响应中断请求(比如退出当前线程的循环体等等)。​​thread1.isInterrupted()​​方法内部直接调用了​​native​​方法,传入的​​ClearInterrupted​​参数是​​false​​,表示不清空中断状态标记:

public boolean isInterrupted() {
return isInterrupted(false);
}
// ClearInterrupted表示是否清楚中断状态标记
private native boolean isInterrupted(boolean ClearInterrupted);
复制代码

因此调用完该方法后,中断标志位不清除。

Thread.interrupted()方法

​Thread.interrupted()​​是定义在​​Thread​​类上的静态方法,用来判断当前线程的中断状态,跟​​thread1.isInterrupted()​​不同的是,该方法返回中断状态之后,会复位(reset)中断状态标记,所谓的复位即恢复默认状态,也可以说是清空中断状态标记。看​​Thread​​类源码可以看到​​Thread.interrupted()​​方法实现非常简单,内部直接调用了​​native​​方法,只不过​​ClearInterrupted​​参数传的是​​true​​,表示清空中断状态标记:

public static boolean interrupted() {
return currentThread().isInterrupted(true);
}
// ClearInterrupted表示是否清楚中断状态标记
private native boolean isInterrupted(boolean ClearInterrupted);
复制代码

可以看出thread1.isInterrupted()​和​Thread.interrupted()​的区别其实就在于获取完中断状态标记之后,是否复位。大家可以根据需要进行选择使用。

2 不考虑线程阻塞时如何优雅的停止一个线程

要想优雅的停止某个线程的运行,需要前面介绍的中断机制。比如两个线程​​a​​和​​b​​,当线程​​a​​想中断线程​​b​​时,可以通过调用​​b.interrupt()​​方法,来设置线程​​b​​的中断标记,以达到通知线程​​b​​的目的。而线程​​b​​需要不断的检查自己的中断标记,以随时响应其他线程的中断,比如下面的实现所示:

public class TestMain {
public static void main(String[] args) throws InterruptedException {
Thread b = new Thread(new Runnable() {
@Override
public void run() {
int num = 0;
while(true) {
if (Thread.interrupted()) {
break;
}
System.out.println("thread b running, num is:" + num++);
}
}
});
b.start();
// 主线程sleep 1ms,让线程b循环一小会
Thread.sleep(1);
// 中断线程b
b.interrupt();
}
}
复制代码

这里在​​主线程​​内新创建了一个线程​​b​​,线程​​b​​内部是一个循环体,每次循环开始都检查一下中断状态,确认是否被中断,如果被中断了即退出循环,结束线程的执行。主线程sleep了1ms,让线程b先循环几次。接着主线程通过​​b.interrupt()​​对线程​​b​​进行中断。运行程序可以看到输出如下(不同机器运行结果不一样):

thread b running, num is:0
thread b running, num is:1
thread b running, num is:2
...
thread b running, num is:25
thread b running, num is:26
thread b running, num is:27

Process finished with exit code 0
复制代码

可以看到主线程成功的中断了线程​​b​​。当然,主线程之所以能成功的中断线程​​b​​,是因为线程​​b​​一直在检查自己的中断状态(如果线程​​b​​太自我,不考虑其他线程,只考虑自己运行,那主线程就无法成功打断线程​​b​​了)。

3 考虑线程阻塞时如何优雅的停止一个线程

前面我们成功的在主线程中中断了线程​​b​​,然后如果线程​​b​​中存在阻塞,比如下面的代码所示,线程​​b​​在sleep时被主线程中断:

public class TestMain {
public static void main(String[] args) throws InterruptedException {
Thread b = new Thread(new Runnable() {
@Override
public void run() {
int num = 0;
while(true) {
if (Thread.interrupted()) {
break;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// Thread.currentThread().interrupt();
e.printStackTrace();
}
System.out.println("thread b running, num is:" + num++);
}
}
});
b.start();
// 主线程sleep5.5秒,让线程b循环5次
Thread.sleep(5500);
// 中断线程b
b.interrupt();
}
}
复制代码

这时线程​​b​​会抛出​​InterruptedException​​异常,上面的代码中我们仅仅打印了下该异常,相当于什么都没做。运行该代码结果如下:

thread b running, num is:0
thread b running, num is:1
thread b running, num is:2
thread b running, num is:3
thread b running, num is:4
java.lang.InterruptedException: sleep interrupted
at java.lang.Thread.sleep(Native Method)
at test.TestMain$1.run(TestMain.java:25)
at java.lang.Thread.run(Thread.java:748)
thread b running, num is:5
thread b running, num is:6
thread b running, num is:7
thread b running, num is:8
thread b running, num is:9
...
复制代码

可以看出,主线程未能成功中断线程​​b​​。

3.1 InterruptedException异常介绍

线程内调用​​wait​​,​​join​​,​​sleep​​时都会进入阻塞状态。当线程处于阻塞状态时被中断,这时线程就会抛出​​InterruptedException​​异常,其实大家可以通俗的理解为一种通知即可。以​​sleep​​方法为例,大家可以按如下模拟实现来理解(底层是native实现):

public static void sleep(long millis) throws InterruptedException 
{
while (/* still waiting for millis to become zero */)
{
if (Thread.interrupted())
{
throw new InterruptedException();
}
// Keep waiting
}
}
复制代码

有了​​InterruptedException​​异常通知,线程就可以在阻塞时立即知道被中断了,进而采取一定的措施响应中断。需要注意的一点是,由于抛出了​​InterruptedException​​异常,因此不会在设置中断标志位。

3.2 考虑线程阻塞时如何优雅的停止一个线程

理解了​​InterruptedException​​异常,我们就可以在线程即使发生阻塞时也能成功进行中断了,如下所示:

public class TestMain {
public static void main(String[] args) throws InterruptedException {
Thread b = new Thread(new Runnable() {
@Override
public void run() {
int num = 0;
while(true) {
if (Thread.interrupted()) {
break;
}
try {
Thread.sleep(1000); // 用sleep来模拟线程的执行
} catch (InterruptedException e) {
Thread.currentThread().interrupt(); // 注意这里是重点!
}
System.out.println("thread b running, num is:" + num++);
}
}
});
b.start();
// 主线程sleep5.5秒,让线程b先循环5次
Thread.sleep(5500);
// 中断线程b
b.interrupt();
}
}
复制代码

这里我们在检查到​​InterruptedException​​异常时,重新设置了中断标志位,这样下次循环一开始时,即可判断被中断了,进而退出循环体。当然我们可以在​​InterruptedException​​异常​​catch​​时直接退出。

4 总结

我们介绍了Java中的线程中断相关知识点,通俗来讲,大家可以理解为中断就是一种线程间相互打断的一种方式,比如两个线程​​a​​和​​b​​,​​a​​如果在某一时刻想打断​​b​​的执行,则可以调用​​b.interrupt()​​方法进行中断,但是要注意,这里仅仅是设置​​b​​的中断状态位,​​b​​看到中断状态位后可以自行决定是否响应,当然,正常情况我们写的代码都需要做好中断状态位的判断(这一点大家在写业务代码时确实经常忽略)。另外对于阻塞中的线程,Java通过​​InterruptedException​​异常来进行通知。

举报

相关推荐

0 条评论