package com.reptileclient.jlifeapp.util;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


@Slf4j
public class ThreadPoolManager {


    public static final int CORE_POOL_SIZE = 20;

    public static final int MAX_POOL_SIZE = 100;

    public static final int KEEP_ALIVE_SECONDS = 300;



    private static final ThreadPoolExecutor THREAD_EXECUTOR = new ThreadPoolExecutor(
            CORE_POOL_SIZE, MAX_POOL_SIZE,
            KEEP_ALIVE_SECONDS, TimeUnit.SECONDS,
            new SynchronousQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());

    private ThreadPoolManager() {
    }


    /**
     * 执行异步任务
     */
    public static Object execute(Runnable task) {
        Future<?> submit = THREAD_EXECUTOR.submit(task);
        try {
            return submit.get();
        } catch (Exception e) {
            log.error("线程池执行任务异常", e);
        }
        return null;
    }


}
