import com.google.common.util.concurrent.ThreadFactoryBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.*;
@Configuration
public class ThreadPool {
Logger logger = LoggerFactory.getLogger(ThreadPool.class);
/**
* 线程池的基本大小
/
static int corePoolSize = 10;
/*
* 线程池最大数量
/
static int maximumPoolSizeSize = 100;
/*
* 线程活动保持时间
/
static long keepAliveTime = 1;
/*
* 任务队列
*/
static ArrayBlockingQueue workQueue = new ArrayBlockingQueue(10);
//可缓存的线程池,如果线程池的容量超过了任务数,自动回收空闲线程,任务增加时可以自动添加新线程,线程池的容量不限制
@Bean
public ExecutorService ExecutorPool(){
logger.info("====线程池启动");
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("thread-call-runner-%d").build();
ExecutorService executor = new ThreadPoolExecutor(corePoolSize,maximumPoolSizeSize,500L,TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(),namedThreadFactory);
return executor;
}
}