public class BoundedExecutorService extends ThreadPoolExecutor
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.
ThreadPoolExecutor.AbortPolicy, ThreadPoolExecutor.CallerRunsPolicy, ThreadPoolExecutor.DiscardOldestPolicy, ThreadPoolExecutor.DiscardPolicy| Constructor and Description |
|---|
BoundedExecutorService(ThreadPoolExecutor 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() |
afterExecute, allowCoreThreadTimeOut, allowsCoreThreadTimeOut, beforeExecute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, terminated, toStringinvokeAll, invokeAll, invokeAny, invokeAny, newTaskFor, newTaskFor, submit, submit, submitpublic BoundedExecutorService(ThreadPoolExecutor base, int max)
public void execute(Runnable r)
execute in interface Executorexecute in class ThreadPoolExecutorpublic void shutdown()
shutdown in interface ExecutorServiceshutdown in class ThreadPoolExecutorpublic List<Runnable> shutdownNow()
shutdownNow in interface ExecutorServiceshutdownNow in class ThreadPoolExecutorpublic boolean isShutdown()
isShutdown in interface ExecutorServiceisShutdown in class ThreadPoolExecutorpublic boolean isTerminated()
isTerminated in interface ExecutorServiceisTerminated in class ThreadPoolExecutorpublic boolean awaitTermination(long timeout,
TimeUnit unit)
throws InterruptedException
awaitTermination in interface ExecutorServiceawaitTermination in class ThreadPoolExecutorInterruptedExceptionCopyright © 2004–2018. All rights reserved.