如何用 interrupt 停止线程

首先介绍一下线程的六大状态

就像生物从出生到长大、最终死亡的过程一样,线程也有自己的生命周期,在 Java 中线程的生命周期中一共有 6 种状态。

New(新创建)

Runnable(可运行)

Blocked(被阻塞)

Waiting(等待)

Timed Waiting(计时等待)

Terminated(被终止)

为什么不强制停止?而是通知、协作

对于 Java 而言,最正确的停止线程的方式是使用 interrupt。但 interrupt 仅仅起到通知被停止线程的作用。而对于被停止的线程而言,它拥有完全的自主权,它既可以选择立即停止,也可以选择一段时间后停止,也可以选择压根不停止。

程序间能够相互通知、相互协作地管理线程,因为如果不了解对方正在做的工作,贸然强制停止线程就可能会造成一些安全的问题,为了避免造成问题就需要给对方一定的时间来整理收尾工作。比如:线程正在写入一个文件,这时收到终止信号,它就需要根据自身业务判断,是选择立即停止,还是将整个文件写入成功后停止,而如果选择立即停止就可能造成数据不完整,不管是中断命令发起者,还是接收者都不希望数据出现问题。

普通线程终止

//线程终止
    static  class StopThread implements Runnable{
        @Override
        public void run() {
            int count = 0;
            while(!Thread.currentThread().isInterrupted() && count < 1000){
                System.out.println("count = " + count++);
            }
        }
    }
    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(new StopThread());
        thread.start();
        Thread.sleep(1000);
        thread.interrupt();
    }

基于生产者和消费者,线程阻塞中获取到终止指令使得线程终止

package com.example.demo;

import org.springframework.boot.test.context.SpringBootTest;

import java.math.MathContext;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * @author zhangguangxin
 * @date 2020/6/19 14:08
 */
@SpringBootTest
public class Volatile {




    static class Producer implements Runnable{
//        public volatile boolean canceled = false;
        BlockingQueue storage;

        public Producer(BlockingQueue storage){
            this.storage = storage;
        }
        @Override
        public void run() {
            int num = 0;
            try{
                while(num <= 100000 && !Thread.currentThread().isInterrupted()){
                    if(num % 50 == 0){
                        storage.put(num);
                        System.out.println(num +" 是50的倍数,被放到仓库里等着消费");
                        System.out.println("仓库容量:=========="+storage.size());
                    }
                    num++;
                }
            }catch (InterruptedException e){
                e.printStackTrace();
            }finally {
                System.out.println("生产者结束运行");
            }
        }
    }

    static class Constumer {
        BlockingQueue storage;
        public  Constumer(BlockingQueue storage){
            this.storage = storage;
        }
        public boolean needMoreNums() {
            if(Math.random() > 0.97){
                return false;
            }
            return true;
        }
    }

    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue storage = new ArrayBlockingQueue(20);
        Producer producer = new Producer(storage);
        Thread producerThread = new Thread(producer);
        producerThread.start();
        Thread.sleep(500);
        Constumer consumer = new Constumer(storage);
        int i = 0;
        while (consumer.needMoreNums()){
                System.out.println(consumer.storage.take() + "被消费了");
                Thread.sleep(100);
        }

        producerThread.interrupt();
        System.out.println("消费者不需要更多数据了。");

        //一旦消费不需要更多数据了,我们应该让生产者也停下来,但是实际情况却停不下来
//        producer.canceled = true;

//        System.out.println(producer.canceled);
    }


}


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。