在springboot项目中集多线程
1.首先在项目启动类中加入设置线程基本配置项
//多线程配置
@EnableAsync
@Configuration
class TaskPoolConfig {
@Bean("NetmarchThread")
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(200);
executor.setMaxPoolSize(210);
executor.setQueueCapacity(200);
executor.setKeepAliveSeconds(60);
executor.setThreadNamePrefix("NetmarchThread-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
return executor;
}
}
2.使用在方法上使用注解变成多线程方法,类加上@Service注解
@Async("NetmarchThread")
3.调用
@Autowired
public ServiceClass serviceClass;
//调用多线程方法
serviceClass.方法名();