spring boot 线程池
配置线程池
@Configuration
@EnableAsync
public class ExecutorConfig {
@Bean
public Executor asyncExecutor(){
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(10);
return threadPoolTaskExecutor;
}
}
使用线程池
@Service
public class T1Service {
@Async("asyncExecutor")
public void t1() throws InterruptedException {
DateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
Thread.sleep(2000);
System.out.println(dateFormat.format(new Date()));
System.out.println("111");
}
}
for循环调用十次