package com.qimai.yttgrass.scan;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author wangwei
 * @version 1.1.1
 * @date 2019-08-09
 * @Description
 * @email wangwei_5521@163.com
 **/
public class MonitorThreadManager {


    private AtomicInteger monitorNum = new AtomicInteger(0);
    private ExecutorService threadPool = Executors.newFixedThreadPool(1);
    private WaitFinishCallBack callBack;


    public MonitorThreadManager(WaitFinishCallBack callBack) {
        this.callBack = callBack;
    }


    public void startMonitor() {
        if (monitorNum.get() == 0) {
            monitorNum.getAndIncrement();
            threadPool.execute(() -> {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                } finally {
                    monitorNum.getAndDecrement();
                    if (callBack != null) {
                        callBack.finish();
                    }
                }
            });
        }
    }

    public interface WaitFinishCallBack {
        void finish();
    }


}
