package com.zy.acs.gateway;
|
|
import com.google.common.util.concurrent.ThreadFactoryBuilder;
|
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;
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
/**
|
* 总线程
|
* Created by vincent on 2019-04-03
|
*/
|
@Component("executors")
|
public class Executors implements DisposableBean {
|
|
private static final AtomicInteger count = new AtomicInteger();
|
|
private ExecutorService executorService;
|
|
private static final int cpus = Runtime.getRuntime().availableProcessors();
|
|
|
/**
|
* 业务线程池
|
*/
|
public ExecutorService getInstance() {
|
if (null == executorService){
|
executorService = new ThreadPoolExecutor(cpus, cpus << 3,
|
30 , TimeUnit.SECONDS,
|
new LinkedBlockingQueue<>(512),
|
new ThreadFactoryBuilder().setNameFormat("task-pool").build(),
|
new ThreadPoolExecutor.CallerRunsPolicy());
|
}
|
|
return executorService;
|
|
}
|
|
/**
|
* 监听器线程池
|
*
|
*/
|
public static ExecutorService getCache() {
|
return java.util.concurrent.Executors.newFixedThreadPool(cpus >> 1, r -> {
|
Thread thread = new Thread(r);
|
thread.setName("zy-acs-task-factory" + count.getAndIncrement());
|
if (thread.isDaemon()) {
|
thread.setDaemon(Boolean.FALSE);
|
}
|
thread.setPriority(Thread.NORM_PRIORITY);
|
|
return thread;
|
});
|
|
}
|
|
|
@Override
|
public void destroy() {
|
if (executorService != null && !executorService.isShutdown()) {
|
executorService.shutdown();
|
}
|
}
|
|
}
|