package com.zy.acs.manager.core.service; import com.google.common.util.concurrent.ThreadFactoryBuilder; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.DisposableBean; import org.springframework.stereotype.Component; import java.util.concurrent.ExecutorService; import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * 总线程 * Created by vincent on 2019-04-03 */ @Slf4j @Component public class ThreadPoolRegulator implements DisposableBean { private static final int cpus = Runtime.getRuntime().availableProcessors(); private ExecutorService executorService; public ExecutorService getInstance() { if (null == executorService){ executorService = new ThreadPoolExecutor( cpus, // number of core threads cpus << 3, // max count ===>> recycle 5 , TimeUnit.SECONDS, new LinkedBlockingQueue<>(512), new ThreadFactoryBuilder().setNameFormat("Matrix-Deal-Pool").build(), new ThreadPoolExecutor.CallerRunsPolicy()); } return executorService; } @Override public void destroy() { if (executorService != null && !executorService.isShutdown()) { executorService.shutdown(); } } }