1.创建配置类
/**
* 线程池配置
* @author Administrator
*
*/
@Configuration
@ComponentScan("com.xxx.xxx.service")//指定扫描任务执行类
//开始异步支持
@EnableAsync
public class AsyncConfig implements AsyncConfigurer{
@Override
public Executor getAsyncExecutor() {
//线程池
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(5);
taskExecutor.setMaxPoolSize(10);
taskExecutor.setQueueCapacity(25);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}}
2.编写线程执行类
@Service
public class AsyncService {
private static final Logger logger = LoggerFactory.getLogger(AsyncService.class);
@Async
public void run(){
//需要执行的代码
}
}
3.启动类添加注解:@EnableAsync