ThreadPoolExecutor에 대해 정리한 내용
병렬 작업 처리가 많아지면 스레드 개수가 증가
- Context Switching 으로 인한 성능 저하
- 지속적인 Thread 생성으로 인한 자원 낭비
쓰레드를 미리 만들어 놓고 재사용하는 방식 사용 (Thread Pool)
ThreadPoolExecutor(int corePoolSize,
int maximumPoolSize,
long keepAliveTime,
TimeUnit unit,
BlockingQueue<Runnable> workQueue)
5개의 파라미터 값
1. corePoolSize
the number of threads to keep in the pool
2. maximumPoolSize
maximum number of threads to allow in the pool keepAliveTime when the number of threads is greater than the corePoolSize
3. keepAliveTime
the maximum time that excess idle threadswill wait for new tasks before terminating
4. unit
the time unit for the keepAliveTime
5. workQueue
the queue to use for holding tasks before they are executed
corePooSize = 3
maximumPoolSize = 50
keepAliveTime = 60
unit = TimeUnit.SECONDS
workQueue = 5(size)
Number of threads required = 30
BlockingQueue<Runnable> blockingQueue = new LinkedBlockingQueue<>();
ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(3, 50, 60, TimeUnit.SECONDS, blockingQueue);
for (execution conditions){
threadPoolExecutor.submit(()-> {
try{
//Code Area
}catch(Exception e){
logger.info("Error"+e.toString());
}
});
}
https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html