如何用 interrupt 停止线程

本文介绍了Java线程的六种状态,包括New、Runnable、Blocked、Waiting、Timed Waiting和Terminated。重点讨论了为何不直接强制停止线程,而是采用interrupt进行协作通知的原因,以避免潜在的安全问题。线程在接收到interrupt信号后可以自主决定何时停止,并举例说明在生产者消费者模型中如何利用interrupt终止线程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先介绍一下线程的六大状态
就像生物从出生到长大、最终死亡的过程一样,线程也有自己的生命周期,在 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);
    }


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值