반응형

ExecutorService


1. newFixedThreadPool vs newCachedThreadPool

일단 doc 문서를 참고 하자면 아래와 같이 설명이 되어있다..

newFixedThreadPool();

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly shutdown.


newCachedThreadPool();

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available. These pools will typically improve the performance of programs that execute many short-lived asynchronous tasks. Calls to execute will reuse previously constructed threads if available. If no existing thread is available, a new thread will be created and added to the pool. Threads that have not been used for sixty seconds are terminated and removed from the cache. Thus, a pool that remains idle for long enough will not consume any resources. Note that pools with similar properties but different details (for example, timeout parameters) may be created using ThreadPoolExecutor constructors.


newFixedThreadPool은 pool size가 다 차고 나면 다음 thread가 실행되기 위해서는 먼저 실행되었던 thread가 종료되어야 한다. 

newCachedThreadPool는 기존에 사용되던 thread가 사용가능하면 재사용하고, 아니면 새로운 thread를 생성한다. 그리고 일정시간 동안 사용하지 않는 thread는 종료시킨다. 


이런 의미에서 newChacedThreadPool이 성능면에서 좋다는 의미인것 같다. 


execute vs submit


728x90
반응형

+ Recent posts