美文网首页
ThreadPoolsUtil

ThreadPoolsUtil

作者: jumper996 | 来源:发表于2019-05-22 19:00 被阅读0次
import org.springframework.stereotype.Component;

import java.util.concurrent.*;

public class ThreadPoolsUtil {

    public static ThreadPoolExecutor threadPool;

    /**
     * 无返回值直接执行, 管他娘的
     * @param runnable
     */
    public  static void execute(Runnable runnable){
        getThreadPool().execute(runnable);
    }

    /**
     * 返回值直接执行, 管他娘的
     * @param callable
     */
    public  static <T> Future<T> submit(Callable<T> callable){
        return   getThreadPool().submit(callable);
    }


    /**
     * dcs获取线程池
     * @return 线程池对象
     */
    public static ThreadPoolExecutor getThreadPool() {
        if (threadPool != null) {
            return threadPool;
        } else {
            synchronized (ThreadPoolsUtil.class) {
                if (threadPool == null) {
                    int size = Runtime.getRuntime().availableProcessors();
                    threadPool = new ThreadPoolExecutor(size, size*2, 60, TimeUnit.SECONDS,
                            new LinkedBlockingQueue<>(), new ThreadPoolExecutor.CallerRunsPolicy());
                }
                return threadPool;
            }
        }
    }
}

相关文章

网友评论

      本文标题:ThreadPoolsUtil

      本文链接:https://www.haomeiwen.com/subject/voqpzqtx.html