package com.tzld.piaoquan.recommend.server.framework.utils;


import lombok.Data;

import java.util.Timer;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * 线程池Helper
 *   可以命名线程池; 保存ThreadPoolExecutor实例, 方便监控线程池信息
 */
@Data
public class ThreadPoolHelper {
    private String name;
    private ThreadPoolExecutor threadPoolExecutor;
    private Timer counterTimer;

    ThreadPoolHelper(ThreadPoolExecutor threadPoolExecutor) {
        this.threadPoolExecutor = threadPoolExecutor;
        this.name = "";
    }

    ThreadPoolHelper(ThreadPoolExecutor threadPoolExecutor, String name) {
        this.threadPoolExecutor = threadPoolExecutor;
        this.name = name;
    }


    public long getTaskCount() {
        return this.threadPoolExecutor.getTaskCount();
    }

    public long getActiveCount() {
        return this.threadPoolExecutor.getActiveCount();
    }

    public long getCorePoolSize() {
        return this.threadPoolExecutor.getCorePoolSize();
    }

    public long getPoolSize() {
        return this.threadPoolExecutor.getPoolSize();
    }

    public long getMaximumPoolSize() {
        return this.threadPoolExecutor.getMaximumPoolSize();
    }

    public long getLargestPoolSize() {
        return this.threadPoolExecutor.getLargestPoolSize();
    }

    public long getTaskQueueSize() {
        return this.threadPoolExecutor.getQueue().size();
    }

    public void cancelCounter() {
        if (counterTimer != null) {
            counterTimer.cancel();
            counterTimer.purge();
        }
    }
}
