流程图:
多线程设计模式:两阶段终止模式

第一版(后续会优化)

@Slf4j
public class TwoPhaseTermination {

    private Thread monitor;

    public void start(){
        monitor = new Thread(()->{
            Thread current = Thread.currentThread();
            while(true){

                if(current.isInterrupted()){
                    log.info("after handler");
                    break;
                }
                try {
                    TimeUnit.SECONDS.sleep(1);
                    log.info("monitor");
                } catch (InterruptedException e) {
                    e.printStackTrace();
                    current.interrupt();
                }
            }

        });
        monitor.start();
    }

    public void stop(){
        monitor.interrupt();
    }
}
class Inner{
    public static void main(String[] args) throws InterruptedException {
        TwoPhaseTermination termination = new TwoPhaseTermination();
        termination.start();
        TimeUnit.SECONDS.sleep(5);
        termination.stop();
    }
}

相关文章:

  • 2021-09-04
  • 2021-06-08
  • 2021-08-01
  • 2021-09-27
  • 2021-08-31
  • 2021-06-19
  • 2021-12-02
  • 2021-08-30
猜你喜欢
  • 2021-06-28
  • 2021-12-05
  • 2021-05-28
  • 2021-06-22
  • 2021-07-28
相关资源
相似解决方案