TaskConfig.java 960 B

12345678910111213141516171819202122232425262728
  1. package thyyxxk.webserver.config;
  2. import org.springframework.context.annotation.Bean;
  3. import org.springframework.context.annotation.Configuration;
  4. import org.springframework.core.task.TaskExecutor;
  5. import org.springframework.scheduling.annotation.EnableAsync;
  6. import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
  7. import java.util.concurrent.ThreadPoolExecutor;
  8. @Configuration
  9. @EnableAsync
  10. public class TaskConfig {
  11. @Bean
  12. public TaskExecutor taskExecutor() {
  13. ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
  14. executor.setCorePoolSize(8);
  15. executor.setMaxPoolSize(16);
  16. executor.setQueueCapacity(50);
  17. executor.setKeepAliveSeconds(120);
  18. executor.setThreadNamePrefix("web-server-async-");
  19. executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
  20. executor.setWaitForTasksToCompleteOnShutdown(true);
  21. return executor;
  22. }
  23. }