优雅的关闭一个线程

首先看几个Thread的方法

stop  强行停止线程 已被废弃掉该方法

interrupt 方法不能直接停止线程,只是给线程打上一个需要停止的标记,具体什么时候停 不确定

isInterrupted  判断线程是否打上了停止的标记 为true表示打上了 但是不表示线程停止了;

 

public class Thread1 implements Runnable {

    @Override
    public void run() {

      while (true){
          try { System.out.println("进入 thread1");
          } catch (Exception e) {
              e.printStackTrace();
          }
          System.out.println("thread1 结束");
      }
    }
}
 public static void main(String[] args) {
        Thread thread1 = new Thread(new Thread1());
        thread1 .start();
        thread1.interrupt();
        boolean interrupted = thread1.isInterrupted();
        System.out.println(interrupted);

    }


hread1 结束
进入 thread1
thread1 结束
进入 thread1
true
thread1 结束
进入 thread1

 

 

下面十段模拟心跳监测的代码

@Component
public class HeardCheck {
    private  Thread threadCheck;
    private  volatile Boolean toStop=false;

    public void heard(){
        threadCheck=new Thread(new Runnable() {
            @Override
            public void run() {
                while (!toStop){
                    System.out.println("当前时间:"+ System.currentTimeMillis());
                    try{  TimeUnit.SECONDS.sleep(10);}catch(Exception e){
                        e.printStackTrace();
                    }

                }
            }
        });
        threadCheck.setDaemon(true);//设置该线程为守候线程
        threadCheck.setName("check_1");// 设置线程的名字
        threadCheck.start();
    }
    public void stop(){
        this.toStop=true;
        System.out.println("停止线程:"+threadCheck.getName() );
    }

    public Thread getThreadCheck() {
        return threadCheck;
    }

    public void setThreadCheck(Thread threadCheck) {
        this.threadCheck = threadCheck;
    }

    public Boolean getToStop() {
        return toStop;
    }

    public void setToStop(Boolean toStop) {
        this.toStop = toStop;
    }
}

启动和停止心跳的代码(默认应该在程序启动时就让心跳开始,这个为了方便直接在方法里面调用)

 

@RestController
public class StopThread {
    @Autowired
    private HeardCheck heardCheck;
    @RequestMapping("/stop")
    public void stop(){
        heardCheck.heard();//心跳开始
        heardCheck.stop();// 心跳停止的方法
        Thread threadCheck = heardCheck.getThreadCheck();
        threadCheck.interrupt();// 给线程标记停止
        try{
            threadCheck.join();// 主线程等待子线程执行完成后 再往下执行
        }catch(Exception e){
            e.printStackTrace();
        }
        System.out.println("线程停止了=======");
    }
}

让项目启动之后就开始执行的方法

@Configuration
public class TestBean implements InitializingBean, DisposableBean {
    //InitializingBean DisposableBean 都属于BeanFactory里面的成员
// 加载完成配置文件之后会执行的一个方法
    @Override
    public void afterPropertiesSet() throws Exception {
        HeardCheck check= new HeardCheck();
        check.heard();
    }
//在容器销毁该bean的时候获得一次回调
    @Override
    public void destroy() throws Exception {
        System.out.println("==============destroy");
    }
}

     欢迎大家关注我的微信公众号 您的关注就是我不懈的动力 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值