工作中经常用到线程池,记录一下:
1.先做线程池的配置类
package com.csair.cbd.config;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;import java.util.List;
import java.util.concurrent.Executor;@Configuration
@EnableAsync(proxyTargetClass=true)
public class ExecutorConfig {
private static Logger logger = LoggerFactory
.getLogger(ExecutorConfig.class);
public ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
@Value("${lead.thread.pool}")
private int pool;
@Bean
public Executor asyncServiceExecutor() {
logger.info("start asyncServiceExecutor");//配置核心线程数
executor.setCorePoolSize(pool);
//配置最大线程数
executor.setMaxPoolSize(pool);
//配置队列大小
executor.setQueueCapacity(99999