package net.northking.cctp.se.thread;

import org.springframework.stereotype.Component;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 附件上传
 */
public class AttachInfoExecutorPool {

    private ThreadPoolExecutor uploadPool;

    private ThreadPoolExecutor savePool;

    public AttachInfoExecutorPool() {
        uploadPool = new ThreadPoolExecutor(1, 3,
                0L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());

        savePool = new ThreadPoolExecutor(1, 3,
                0L, TimeUnit.SECONDS,
                new LinkedBlockingQueue<Runnable>());
    }


    /**
     * 提交执行附件上传任务
     * @param task
     */
    public void uploadTaskSubmit(Runnable task) {
        uploadPool.submit(task);
    }

    /**
     * 线程池是否空闲
     * @return
     */
    public boolean isUploadAvailable() {
        return uploadPool.getActiveCount() == 0;
    }

    /**
     * 队列中任务剩余数量
     * @return
     */
    public int uploadTaskLeft() {
        return uploadPool.getQueue().size();
    }

    /**
     * 提交执行附件保存任务
     * @param task
     */
    public void saveTaskSubmit(Runnable task) {
        savePool.submit(task);
    }

}
