[-, , , ].
, , ThreadPoolExecutor . , ThreadPoolExecutor BlockingQueue.offer(T item), . , true, ( ) false. ThreadPoolExecutor RejectedExecutionHandler, .
javadoc:
. . , , - , , RejectedExecutionHandler.
ThreadPoolExecutor.AbortPolicy(), RejectedExecutionException "" "" ThreadPoolExecutor.
try {
executorService.execute(new Runnable() { ... });
}
catch (RejectedExecutionException e) {
}
- , (DiscardPolicy) , "" "" (CallerRunsPolicy). "" "" , . ( , , ):
ExecutorService service = new ThreadPoolExecutor(..., new ThreadPoolExecutor.CallerRunsPolicy());
, RejectedExecutionHandler, , ( , , )
public class BlockUntilAvailableSlot implements RejectedExecutionHandler {
public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
if (e.isTerminated() || e.isShutdown()) {
return;
}
boolean submitted = false;
while (! submitted) {
if (Thread.currentThread().isInterrupted()) {
}
try {
e.execute(r);
submitted = true;
}
catch (RejectedExceptionException e) {
try {
Thread.sleep(100L);
}
catch (InterruptedException e) {
;
}
}
}
}
}