- 线程池配置类
@Configuration
public class ThreadPoolConfig {
@Bean("threadPoolTaskExecutor_1")
public ThreadPoolTaskExecutor threadPoolTaskExecutor_1() {
System.out.println("线程池threadPoolTaskExecutor1初始化===============>开始:"+ System.currentTimeMillis());
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
threadPoolTaskExecutor.setCorePoolSize(10);
threadPoolTaskExecutor.setMaxPoolSize(30);
threadPoolTaskExecutor.setQueueCapacity(100);
threadPoolTaskExecutor.setKeepAliveSeconds(60);
threadPoolTaskExecutor.setThreadNamePrefix("threadPoolTaskExecutor1");
threadPoolTaskExecutor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
threadPoolTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);
threadPoolTaskExecutor.setAwaitTerminationSeconds(60);
threadPoolTaskExecutor.initialize();
System.out.println("线程池threadPoolTaskExecutor1初始化===============>完成:"+ System.currentTimeMillis());
return threadPoolTaskExecutor;
}
}
- 使用线程
public class ThreadPoolTest {
@Async("threadPoolTaskExecutor_1")
public void test01() {
try {
Thread.sleep(5000);
System.out.println("test01当前线程是{}" + Thread.currentThread().getName());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}