《Java并发编程》之线程中断与终止线程运行

Java中启动一个线程很容易,通常情况下我们都是等到任务运行结束后让线程自行停止。但有时需要在任务正在运行时取消他们,使得线程快速结束。对此Java并没有提供任何机制。但是我们可以通过Java提供的线程中断机制来实现。

首先来看Thread类三个和中断有关的方法:

public class Thread {
    // 发出一个中断请求,把标志位设定为中断状态,不会终止线程运行。
// 其他线程试图调用该方法,会检测是否有权限中断该线程(正常情况
// 下不会存在权限问题,这里可以忽略)
public void interrupt() { ... } // 检测标志位是否为中断的状态 public boolean isInterrupted() { ... } // 清除当前线程的标志位的中断状态,返回是否为中断状态 public static boolean interrupted() { ... } ... }

既然线程中断不会终止线程的运行,那么如何通过线程中断来实现终止线程运行呢?

我们知道一些阻塞线程的方法会抛出InterruptedException表示线程中断发生,在这种情况下就可以使用线程中断来终止线程的运行:

public class TestInterrupt {

    public static void main(String[] args)
    {
        BlockingQueue<Object> ObjectQueue = new LinkedBlockingQueue<Object>();
        Consumer consumer = new Consumer(ObjectQueue);
        
        Thread t = new Thread(consumer);
        t.start();
        
        // 等待线程的启动
        try
        {
            Thread.sleep(1000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        
        // 中断线程
        t.interrupt();
    }

}

class Consumer implements Runnable
{
    private final BlockingQueue<Object> ObjectQueue;
    public Consumer(BlockingQueue<Object> ObjectQueue)
    {
        if (ObjectQueue == null)
        {
            throw new IllegalArgumentException("messageQueue cannot be null");
        }
        
        this.ObjectQueue = ObjectQueue;
    }

    @Override
    public void run()
    {
        boolean isRunning = true;
        while (isRunning)
        {
            try
            {
                // take方法阻塞时会因为线程中断抛出中断异常
                System.out.println(ObjectQueue.take());
            }
            catch (InterruptedException e)
            {
          // 一旦抛出中断异常,线程的中断状态就会被清除,这个时候调用
// Thread的isInterrupted()方法返回的是false isRunning
= false; System.out.println("Cancelled"); } } } }

很多任务执行的服务程序的逻辑和上面的例子很类似,都可以使用这种方法来终止线程的运行。

原文地址:https://www.cnblogs.com/wanly3643/p/3992186.html