JAVA이야기

ThreadPoolExecutor

НooпeУ 2011. 12. 7. 22:09
public threadPoolExecutor
(
int corePoolSize,  //최소 스레드 개수 -> 실행할 작업이 없어도 스레드 개수를 최대한 코어개수에 마춘다.
int maximumPoolSize, //스레드 최대 개수
long keepAliveTime,  //스레드 유지시간 
TimeUnit unit,        //시간단위
BlockingQueue<Runnable> workQueue, //작업이 들어있는 큐 스레드들은 여기서 하나씩 꺼내서 작업을 처리한다.
ThreadFactory threadFactory,
RejectedExecutionHandler handler)
{



ThreadPoolExecutor exec = new ThreadPoolExecutor(npages, npages,
0L, TimeUnit.MILLISECONDS, 
new ArrayBlockingQueue<Runnable>(CAPACITY));
exec.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); 

난 이런식으로 사용함... 

Abortpolicy
CallerRunsPolicy
DiscardPolicy
DiscardOldestPolicy

AbortPolicy 기본적으로 사용함 : 큐가 넘치면 RejectedExecutionException을 던지며, 이는 ㄴ호출한 스레드에서 직접 이 예외처리를 해야한다. 

discard 정책은 큐에 작업을 더 이상 쌓을 수 없다면 방금 추가시키려고 했던 정책을 아무 반응 없이 제거한다.
caller run 정책은 작업을 제거해버리거나 예외를 던지지 않으면서 큐의 크기를 초과하는 작업을 프로듀서에게 거꾸로 넘겨 작업 추가 속도를 늦출 수 있도록 일종의 속도 조절 방법으로 사용. 큐에 집어넣으려고 했던 놈이 이일은 대신함...