public class CallerBlocksPolicy
extends java.lang.Object
implements java.util.concurrent.RejectedExecutionHandler
RejectedExecutionHandler
blocks the current thread until the
backing queue for a ThreadPoolExecutor
can accept another task. This
becomes useful for situations where tasks are typically long running or
unknown and it is undesirable to block the task submission thread for the
entire length of one of these tasks (which can easily happen if using the
ThreadPoolExecutor.CallerRunsPolicy
). Provided
your queue of choice for the ThreadPoolExecutor
is bounded, you'll be
able to continue submitting tasks up until the point where the queue is full
and then resume the submitting of new tasks immediately when the queue can
accept more without having to wait on an existing task in the submitting
thread to finish.Constructor and Description |
---|
CallerBlocksPolicy() |
Modifier and Type | Method and Description |
---|---|
void |
rejectedExecution(java.lang.Runnable r,
java.util.concurrent.ThreadPoolExecutor executor)
Instead of throwing away the rejected task, put it back onto the queue
that the
ThreadPoolExecutor is using, effectively blocking
execution until the put() succeeds or results in an
InterruptedException which is wrapped and rethrown as
a RejectedExecutionException . |
public void rejectedExecution(java.lang.Runnable r, java.util.concurrent.ThreadPoolExecutor executor)
ThreadPoolExecutor
is using, effectively blocking
execution until the put() succeeds or results in an
InterruptedException
which is wrapped and rethrown as
a RejectedExecutionException
.rejectedExecution
in interface java.util.concurrent.RejectedExecutionHandler
r
- the runnable task requested to be executedexecutor
- the executor attempting to execute this task