Package ome.services.scheduler
Class ThreadPool
- java.lang.Object
-
- java.util.concurrent.AbstractExecutorService
-
- java.util.concurrent.ThreadPoolExecutor
-
- ome.services.scheduler.ThreadPool
-
- All Implemented Interfaces:
java.util.concurrent.Executor
,java.util.concurrent.ExecutorService
public class ThreadPool extends java.util.concurrent.ThreadPoolExecutor
An internal implementation ofThreadPoolExecutor
that additionally handles the submission ofbackground(Callable)
tasks at a lower priority and with more limited slots.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class java.util.concurrent.ThreadPoolExecutor
java.util.concurrent.ThreadPoolExecutor.AbortPolicy, java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy, java.util.concurrent.ThreadPoolExecutor.DiscardPolicy
-
-
Constructor Summary
Constructors Constructor 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.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected void
afterExecute(java.lang.Runnable r, java.lang.Throwable t)
StandardThreadPoolExecutor
extension point which checks for the BackgroundFutureTask marker and releases a slot in themaxBackground
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)
OverridesThreadPoolExecutor
to return our own instance ofFutureTask
so that we have a marker for when the background activity is completed inafterExecute(Runnable, Throwable)
.-
Methods inherited from class java.util.concurrent.ThreadPoolExecutor
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
-
-
-
-
Constructor Detail
-
ThreadPool
public ThreadPool()
Default constructor. Unlike the argument constructor, it effectively has no queue for tasks and will always create a new thread to accommodate new tasks. Background tasks are limited to 10.
-
ThreadPool
public ThreadPool(int minThreads, int maxThreads, long msTimeout, int backgroundThreads, long backgroundTimeout)
This constructor creates a thread pool with an unbounded queue. This means thatminThreads
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 thatmaxThreads
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, somsTimeout
also does nothing. See theThreadPoolExecutor
docs for more information.- Parameters:
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 tominThreads
in size, so ifminThreads
is lower, that will control the maximum number of threads capable of running background tasks.backgroundTimeout
- If more thanbackgroundThreads
tasks are queued or processing, this is how long a task will wait to be submitted before being dropped
-
-
Method Detail
-
getExecutor
@Deprecated public java.util.concurrent.ExecutorService getExecutor()
Deprecated.Returns this. PreviouslyThreadPool
was not itself anExecutorService
and returned a delegate instead, most typically in a Spring configuration.
-
background
public <T> java.util.concurrent.Future<T> background(java.util.concurrent.Callable<T> callable)
Schedule a task in one of the limited background slots. If scheduling takes more than hour then the submission will be rejected. Otherwise, the task will run in the same thread pool as both USER and BACKGROUND tasks.- Parameters:
callable
-- Returns:
- a future for this task
-
newTaskFor
protected <T> java.util.concurrent.RunnableFuture<T> newTaskFor(java.util.concurrent.Callable<T> callable)
OverridesThreadPoolExecutor
to return our own instance ofFutureTask
so that we have a marker for when the background activity is completed inafterExecute(Runnable, Throwable)
. Overriding this method is somewhat non-standard.- Overrides:
newTaskFor
in classjava.util.concurrent.AbstractExecutorService
-
afterExecute
protected void afterExecute(java.lang.Runnable r, java.lang.Throwable t)
StandardThreadPoolExecutor
extension point which checks for the BackgroundFutureTask marker and releases a slot in themaxBackground
Semaphore
.- Overrides:
afterExecute
in classjava.util.concurrent.ThreadPoolExecutor
-
-