package org.example;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TwoStageStop {
    public static void main(String[] args) throws InterruptedException {
        TwoStageStop twoStageStop = new TwoStageStop();
        twoStageStop.start();
        Thread.sleep(5000);
        twoStageStop.stop();
    }
    private Thread monitorThread;
    private volatile boolean stop = false;
    public void start() {
        monitorThread = new Thread(()->{
            while (true) {
                if(stop) {
                    log.info("料理后事");
                    break;
                }
                try {
                    Thread.sleep(1000);
                    log.info("执行监控流程");
                }catch (InterruptedException e){

                }
            }
        },"monitor");
        monitorThread.start();

    }
    public void stop(){
        stop = true;
        monitorThread.interrupt();
    }
}