線程基本介紹和創(chuàng)建方式
jdk自帶的線程池
java.util.concurrent包提供了線程相關(guān)的類,ThreadPoolExecutor用于創(chuàng)建一個線程池,通常我們會這樣創(chuàng)建一個線程池
private ThreadPoolExecutor scheduleExecutor =
new ThreadPoolExecutor(100, 100, 0, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100),
new NamedThreadFactory("basic-data"), new LoggerDiscardPolicy());
spring的線程池
spring的線程池是對ThreadPoolExecutor的封裝
@Configuration
public class ThreadPoolConfig {
@Bean("threadPoolTaskExecutor")
public ThreadPoolTaskExecutor threadPoolTaskExecutor(){
ThreadPoolTaskExecutor threadPoolTaskExecutor=new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(4);
threadPoolTaskExecutor.setKeepAliveSeconds(10);
threadPoolTaskExecutor.setMaxPoolSize(10);
threadPoolTaskExecutor.setQueueCapacity(10);
return threadPoolTaskExecutor;
}
}