Spring에서는 비동기 작업을 처리하기 위해 @Async와 함께 스레드 풀을 설정할 수 있습니다.
기본적으로
SimpleAsyncTaskExecutor가 사용되지만, 성능 최적화를 위해 ThreadPoolTaskExecutor를 직접 설정하는 것이 추천됩니다.
@Bean(name = "customTaskExecutor")
public Executor customTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(8); // 기본적으로 유지할 최소 스레드 개수
executor.setMaxPoolSize(16); // 최대 스레드 개수
executor.setQueueCapacity(500); // 큐에 저장할 작업 개수 (초과 시 새 스레드 생성)
executor.setThreadNamePrefix("Async-"); // 스레드 이름 접두사
executor.initialize();
return executor;
}
SimpleAsyncTaskExecutor 사용됨)만약 위와 같이 스레드 풀을 설정하지 않는다면 Spring Boot는 기본적으로 **SimpleAsyncTaskExecutor**를 사용합니다.
단점 (반드시!!!!! 설정해서 사용하자)
ThreadPoolExecutor가 아님)@Configuration
@EnableAsync
public class AsyncConfig {
}
ExecutorService executor = Executors.newFixedThreadPool(10);
특징
n)의 스레드를 유지