public class ThreadPool
extends java.util.concurrent.ThreadPoolExecutor
ThreadPoolExecutor
that additionally
handles the submission of background(Callable)
tasks at a lower
priority and with more limited slots.java.util.concurrent.ThreadPoolExecutor.AbortPolicy, java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardPolicy
Constructor and Description |
---|
ThreadPool()
Default constructor.
|
ThreadPool(int minThreads,
int maxThreads,
long msTimeout,
int backgroundThreads,
long backgroundTimeout)
This constructor creates a thread pool with an unbounded queue.
|
Modifier and Type | Method and Description |
---|---|
protected void |
afterExecute(java.lang.Runnable r,
java.lang.Throwable t)
Standard
ThreadPoolExecutor extension point which checks for the
BackgroundFutureTask marker and releases a slot in the maxBackground
Semaphore . |
<T> java.util.concurrent.Future<T> |
background(java.util.concurrent.Callable<T> callable)
Schedule a task in one of the limited background slots.
|
java.util.concurrent.ExecutorService |
getExecutor()
Deprecated.
|
protected <T> java.util.concurrent.RunnableFuture<T> |
newTaskFor(java.util.concurrent.Callable<T> callable)
Overrides
ThreadPoolExecutor to return our own instance of
FutureTask so that we have a marker for when the background
activity is completed in afterExecute(Runnable, Throwable) . |
allowCoreThreadTimeOut, allowsCoreThreadTimeOut, awaitTermination, beforeExecute, execute, finalize, getActiveCount, getCompletedTaskCount, getCorePoolSize, getKeepAliveTime, getLargestPoolSize, getMaximumPoolSize, getPoolSize, getQueue, getRejectedExecutionHandler, getTaskCount, getThreadFactory, isShutdown, isTerminated, isTerminating, prestartAllCoreThreads, prestartCoreThread, purge, remove, setCorePoolSize, setKeepAliveTime, setMaximumPoolSize, setRejectedExecutionHandler, setThreadFactory, shutdown, shutdownNow, terminated, toString
public ThreadPool()
public ThreadPool(int minThreads, int maxThreads, long msTimeout, int backgroundThreads, long backgroundTimeout)
minThreads
will set the number of core threads,
which is the maximum number of threads active at one time when an
unbounded queue is used. This also means that maxThreads
does
nothing, since threads are created beyond the core pool size only when the
queue is full. Additionally, by default core threads never time out,
so msTimeout
also does nothing.
See the ThreadPoolExecutor
docs for more information.minThreads
- Sets the core pool size which is also the MAX pool sizemaxThreads
- This does NOTHINGmsTimeout
- This does NOTHINGbackgroundThreads
- Parameter name is a bit misleading. It is
the maximum number of background tasks that can be submitted
(queued or running) at once. The background threads come from the same
pool, which is limited to minThreads
in size, so if
minThreads
is lower, that will control the maximum number
of threads capable of running background tasks.backgroundTimeout
- If more than backgroundThreads
tasks are queued or processing, this is how long a task will wait
to be submitted before being dropped@Deprecated public java.util.concurrent.ExecutorService getExecutor()
ThreadPool
was not itself an
ExecutorService
and returned a delegate instead, most typically
in a Spring configuration.public <T> java.util.concurrent.Future<T> background(java.util.concurrent.Callable<T> callable)
callable
- protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor(java.util.concurrent.Callable<T> callable)
ThreadPoolExecutor
to return our own instance of
FutureTask
so that we have a marker for when the background
activity is completed in afterExecute(Runnable, Throwable)
.
Overriding this method is somewhat non-standard.newTaskFor
in class java.util.concurrent.AbstractExecutorService
protected void afterExecute(java.lang.Runnable r, java.lang.Throwable t)
ThreadPoolExecutor
extension point which checks for the
BackgroundFutureTask marker and releases a slot in the maxBackground
Semaphore
.afterExecute
in class java.util.concurrent.ThreadPoolExecutor