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
'Development > Java' 카테고리의 다른 글
Fileld명을 테이블 컬럼명으로 바꾸자 (0) | 2013.04.06 |
---|---|
Tomcat 구동시 라이브러리를 못찾을 경우.. (0) | 2013.03.29 |
오버라이딩 규칙 (0) | 2013.02.12 |
Java에서 Null 값을 비교할 때 (0) | 2013.02.12 |
Deep Copy vs Shallow Copy (0) | 2013.02.12 |