머리말
Github에서 : https://github.com/yihonglei/thinking-in-concurrent
전략 중단
새 작업 제출은 예외가 호출자에 의해 캡처 될 때 기본 정책, 직접 던져 RejectedExecutionException을 선택하지.
new ThreadPoolExecutor.AbortPolicy()
/**
* A handler for rejected tasks that throws a
* {@code RejectedExecutionException}.
*/
public static class AbortPolicy implements RejectedExecutionHandler {
/**
* Creates an {@code AbortPolicy}.
*/
public AbortPolicy() { }
/**
* Always throws RejectedExecutionException.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
* @throws RejectedExecutionException always
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
throw new RejectedExecutionException("Task " + r.toString() +
" rejected from " +
e.toString());
}
}
두 CallerRuns 전략
작업이 슬로우되지 않는 포기하지만, 일부 작업은 호출자에게 떨어질 것이다하지 메커니즘을 조정합니다. 그것은 스레드 풀의 스레드에서 새 작업을 수행하지 않습니다
그러나 구현을 담당하는 새 작업 호출 exector 스레드입니다 튜닝을 실행합니다.
new ThreadPoolExecutor.CallerRunsPolicy()
/**
* A handler for rejected tasks that runs the rejected task
* directly in the calling thread of the {@code execute} method,
* unless the executor has been shut down, in which case the task
* is discarded.
*/
public static class CallerRunsPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code CallerRunsPolicy}.
*/
public CallerRunsPolicy() { }
/**
* Executes task r in the caller's thread, unless the executor
* has been shut down, in which case the task is discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
r.run();
}
}
}
세 가지 전략을 폐기
새 작업 제출은 포기되었다.
new ThreadPoolExecutor.DiscardPolicy()
/**
* A handler for rejected tasks that silently discards the
* rejected task.
*/
public static class DiscardPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardPolicy}.
*/
public DiscardPolicy() { }
/**
* Does nothing, which has the effect of discarding task r.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
}
}
네 DiscardOldest 전략
큐 작업은 "팀의 머리"입니다, 다음 새 작업을 제출하려고합니다. 작업 헤드 (작업 큐 우선 순위 큐 현장에 적합하지 않음) 삭제됩니다
new ThreadPoolExecutor.DiscardOldestPolicy()
/**
* A handler for rejected tasks that discards the oldest unhandled
* request and then retries {@code execute}, unless the executor
* is shut down, in which case the task is discarded.
*/
public static class DiscardOldestPolicy implements RejectedExecutionHandler {
/**
* Creates a {@code DiscardOldestPolicy} for the given executor.
*/
public DiscardOldestPolicy() { }
/**
* Obtains and ignores the next task that the executor
* would otherwise execute, if one is immediately available,
* and then retries execution of task r, unless the executor
* is shut down, in which case task r is instead discarded.
*
* @param r the runnable task requested to be executed
* @param e the executor attempting to execute this task
*/
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (!e.isShutdown()) {
e.getQueue().poll();
e.execute(r);
}
}
}
다섯 사용자 정의 포화 처리 전략
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 循环处理,当队列有空位时,该任务进入队列,等待线程池处理
*/
public class CustomRejectedExecutionHandler implements RejectedExecutionHandler {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
try {
executor.getQueue().put(r);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}