public class BoundedExecutorService extends AbstractExecutorService
Executor
so that we only ask the wrapped Executor to execute N number of tasks
at any given time.
The intention is to use this with ThreadPoolExecutor
with SynchronousQueue
with unbounded max capacity (so that for up to N tasks we keep creating more threads for work,
but beyond that we start to push the tasks into the queue of an infinite capacity.)
This is necessary because ThreadPoolExecutor
tries to push work into the queue
first and only create more threads once the queue is full, so for a queue with infinite
capacity it'll never create threads beyond the core pool size.
See http://www.kimchy.org/juc-executorservice-gotcha/ for more discussion of this.
Because there's no call back to tell us when the wrapped ExecutorService
has
finished executing something, this class needs to hand out the next task slightly
before the wrapped ExecutorService
is done with the previous task. The net result
is that the wrapped ExecutorService
will end up running N+1 threads (of which
1 is almost always idle.) I'm not sure how to fix this.
Constructor and Description |
---|
BoundedExecutorService(ExecutorService base,
int max) |
Modifier and Type | Method and Description |
---|---|
boolean |
awaitTermination(long timeout,
TimeUnit unit) |
void |
execute(Runnable r) |
boolean |
isShutdown() |
boolean |
isTerminated() |
void |
shutdown() |
List<Runnable> |
shutdownNow() |
invokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submit
public BoundedExecutorService(ExecutorService base, int max)
public void execute(Runnable r)
public void shutdown()
public boolean isShutdown()
public boolean isTerminated()
public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException
InterruptedException
Copyright © 2015. All rights reserved.