Class BlockingExecutorService
- java.lang.Object
-
- com.pervasivecode.utils.concurrent.executors.BlockingExecutorService
-
- All Implemented Interfaces:
Executor
,ExecutorService
public class BlockingExecutorService extends Object implements ExecutorService
Wrap a ThreadPoolExecutor with an ExecutorService implementation that blocks the thread attempting to submit a task when the task queue is full, rather than rejecting the submitted task (asThreadPoolExecutor
does).This can be useful in applications where there is a finite but large number of tasks to be processed by the ExecutorService, so the task queue of pending submitted tasks would have to be unreasonably large to prevent the thread(s) generating
Runnable
orCallable
instances from experiencing task rejection (aRejectedExecutionException
). With aThreadPoolExecutor
this would result in rejected tasks. With this class, the task-submitting thread can be throttled simply by the fact thatsubmit(java.util.concurrent.Callable<T>)
will block until the work queue of the enclosedThreadPoolExecutor
has enough space to enqueue the task.A similar effect can be produced by the
ThreadPoolExecutor
'sThreadPoolExecutor.CallerRunsPolicy
, but in that case, the decision to run tasks on the task-generating thread creates situations where a long-running task would keeping the task-generating caller busy, resulting in threads in theThreadPoolExecutor
finishing their own tasks and then being starved for new tasks. This drawback could be partly mitigated by lengthening the work queue (to try and ensure that there are enough pending tasks in the queue to keep the worker threads busy until the work-generating thread is finished and can fill the queue with more tasks) but that means the caller has to estimate how big the queue will need to be based on how long a task will take to finish.This class embodies a simpler solution from the caller's perspective, which is to just block the task-generating thread(s) while the queue is full, allowing it to run whenever the queue has available space to fill.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
BlockingExecutorService.Operation
The kind of time-consuming activity that the BlockingExecutorService is engaged in, for use as a timer-type value in anMultistageStopwatch
that is measuring how much time is being spent in each activity.
-
Constructor Summary
Constructors Constructor Description BlockingExecutorService(BlockingExecutorServiceConfig config)
Create a BlockingExecutorService with the specified configuration.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
awaitTermination(long timeout, TimeUnit unit)
void
execute(Runnable command)
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks)
<T> List<Future<T>>
invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
<T> T
invokeAny(Collection<? extends Callable<T>> tasks)
<T> T
invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit)
boolean
isShutdown()
boolean
isTerminated()
void
shutdown()
List<Runnable>
shutdownNow()
Shut down this executor service, and attempt to cancel running tasks.Future<?>
submit(Runnable task)
<T> Future<T>
submit(Runnable task, T result)
<T> Future<T>
submit(Callable<T> task)
-
-
-
Constructor Detail
-
BlockingExecutorService
public BlockingExecutorService(BlockingExecutorServiceConfig config)
Create a BlockingExecutorService with the specified configuration.- Parameters:
config
- An object containing configuration information for the BlockingExecutorService instance being created.
-
-
Method Detail
-
shutdown
public void shutdown()
- Specified by:
shutdown
in interfaceExecutorService
-
shutdownNow
public List<Runnable> shutdownNow()
Shut down this executor service, and attempt to cancel running tasks. Tasks that have not started will be returned. Note: the returned tasks are wrapper Runnable instances that perform internal queue and timer bookkeeping for this executor service, in addition to executing a Runnable or Callable task that a caller submitted viaexecute
,submit
,invokeAll
, orinvokeAny
.As a result, none of the submitted tasks will appear as elements of the returned list; that is, the intersection of the set of submitted tasks and the set of tasks returned from this method is the null set.
(Also, due to internal implementation details, there may be additional housekeeping tasks present in the list of tasks that is returned, which do not directly correspond to any tasks that a caller submitted.)
- Specified by:
shutdownNow
in interfaceExecutorService
- Returns:
- A list of wrapper Runnable instances that execute the submitted tasks (Runnable or Callable instances).
-
isShutdown
public boolean isShutdown()
- Specified by:
isShutdown
in interfaceExecutorService
-
isTerminated
public boolean isTerminated()
- Specified by:
isTerminated
in interfaceExecutorService
-
awaitTermination
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
awaitTermination
in interfaceExecutorService
- Throws:
InterruptedException
-
submit
public <T> Future<T> submit(Callable<T> task)
- Specified by:
submit
in interfaceExecutorService
-
submit
public <T> Future<T> submit(Runnable task, T result)
- Specified by:
submit
in interfaceExecutorService
-
submit
public Future<?> submit(Runnable task)
- Specified by:
submit
in interfaceExecutorService
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
- Specified by:
invokeAll
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAll
public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
- Specified by:
invokeAll
in interfaceExecutorService
- Throws:
InterruptedException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
- Specified by:
invokeAny
in interfaceExecutorService
- Throws:
InterruptedException
ExecutionException
-
invokeAny
public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
- Specified by:
invokeAny
in interfaceExecutorService
- Throws:
InterruptedException
ExecutionException
TimeoutException
-
-