@ManagedObject(value="A thread pool") public class QueuedThreadPool extends ContainerLifeCycle implements ThreadPool.SizedThreadPool, Dumpable, TryExecutor
Modifier and Type | Class and Description |
---|---|
private class |
QueuedThreadPool.Runner |
AbstractLifeCycle.AbstractLifeCycleListener
ThreadPool.SizedThreadPool
TryExecutor.NoTryExecutor
Container.InheritedListener, Container.Listener
LifeCycle.Listener
Modifier and Type | Field and Description |
---|---|
private ThreadPoolBudget |
_budget |
private AtomicBiInteger |
_counts
Encodes thread counts:
HiTotal thread count or Integer.MIN_VALUE if stopping
LoNet idle threads == idle threads - job queue size
|
private boolean |
_daemon |
private boolean |
_detailedDump |
private int |
_idleTimeout |
private java.util.concurrent.BlockingQueue<java.lang.Runnable> |
_jobs |
private java.lang.Object |
_joinLock |
private java.util.concurrent.atomic.AtomicLong |
_lastShrink |
private int |
_lowThreadsThreshold |
private int |
_maxThreads |
private int |
_minThreads |
private java.lang.String |
_name |
private int |
_priority |
private int |
_reservedThreads |
private java.lang.Runnable |
_runnable |
private java.lang.ThreadGroup |
_threadGroup |
private java.util.Set<java.lang.Thread> |
_threads |
private TryExecutor |
_tryExecutor |
private static Logger |
LOG |
private static java.lang.Runnable |
NOOP |
FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPING
NO_TRY
Constructor and Description |
---|
QueuedThreadPool() |
QueuedThreadPool(int maxThreads) |
QueuedThreadPool(int maxThreads,
int minThreads) |
QueuedThreadPool(int maxThreads,
int minThreads,
java.util.concurrent.BlockingQueue<java.lang.Runnable> queue) |
QueuedThreadPool(int maxThreads,
int minThreads,
int idleTimeout) |
QueuedThreadPool(int maxThreads,
int minThreads,
int idleTimeout,
java.util.concurrent.BlockingQueue<java.lang.Runnable> queue) |
QueuedThreadPool(int maxThreads,
int minThreads,
int idleTimeout,
java.util.concurrent.BlockingQueue<java.lang.Runnable> queue,
java.lang.ThreadGroup threadGroup) |
QueuedThreadPool(int maxThreads,
int minThreads,
int idleTimeout,
int reservedThreads,
java.util.concurrent.BlockingQueue<java.lang.Runnable> queue,
java.lang.ThreadGroup threadGroup) |
Modifier and Type | Method and Description |
---|---|
private boolean |
addCounts(int deltaThreads,
int deltaIdle) |
protected void |
doStart()
Starts the managed lifecycle beans in the order they were added.
|
protected void |
doStop()
Stops the managed lifecycle beans in the reverse order they were added.
|
void |
dump(java.lang.Appendable out,
java.lang.String indent)
Dump this object (and children) into an Appendable using the provided indent after any new lines.
|
java.lang.String |
dumpThread(long id) |
private void |
ensureThreads() |
void |
execute(java.lang.Runnable job) |
int |
getBusyThreads() |
int |
getIdleThreads() |
int |
getIdleTimeout()
Get the maximum thread idle time.
|
int |
getLowThreadsThreshold() |
int |
getMaxThreads()
Get the maximum number of threads.
|
int |
getMinThreads()
Get the minimum number of threads.
|
java.lang.String |
getName() |
protected java.util.concurrent.BlockingQueue<java.lang.Runnable> |
getQueue() |
int |
getQueueSize()
Get the size of the job queue.
|
int |
getReservedThreads()
Get the number of reserved threads.
|
ThreadPoolBudget |
getThreadPoolBudget() |
int |
getThreads() |
int |
getThreadsPriority()
Get the priority of the pool threads.
|
boolean |
interruptThread(long id) |
boolean |
isDaemon() |
boolean |
isDetailedDump() |
boolean |
isLowOnThreads()
Returns whether this thread pool is low on threads.
|
void |
join()
Blocks until the thread pool is
stopped . |
private void |
joinThreads(long stopByNanos) |
protected java.lang.Thread |
newThread(java.lang.Runnable runnable) |
protected void |
removeThread(java.lang.Thread thread) |
protected void |
runJob(java.lang.Runnable job)
Runs the given job in the
current thread . |
void |
setDaemon(boolean daemon)
Thread Pool should use Daemon Threading.
|
void |
setDetailedDump(boolean detailedDump) |
void |
setIdleTimeout(int idleTimeout)
Set the maximum thread idle time.
|
void |
setLowThreadsThreshold(int lowThreadsThreshold) |
void |
setMaxThreads(int maxThreads)
Set the maximum number of threads.
|
void |
setMinThreads(int minThreads)
Set the minimum number of threads.
|
void |
setName(java.lang.String name) |
void |
setQueue(java.util.concurrent.BlockingQueue<java.lang.Runnable> queue)
Deprecated.
pass the queue to the constructor instead
|
void |
setReservedThreads(int reservedThreads)
Set the number of reserved threads.
|
void |
setThreadPoolBudget(ThreadPoolBudget budget) |
void |
setThreadsPriority(int priority)
Set the priority of the pool threads.
|
protected void |
startThread() |
java.lang.String |
toString() |
boolean |
tryExecute(java.lang.Runnable task)
Attempt to execute a task.
|
addBean, addBean, addBean, addEventListener, addManaged, contains, destroy, dump, dump, dump, dump, dumpBeans, dumpObject, dumpObjects, dumpStdErr, dumpThis, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, start, stop, unmanage, updateBean, updateBean, updateBeans
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
dump, dump, dumpContainer, dumpIterable, dumpMapEntries, dumpObject, dumpObjects, dumpSelf, named
asTryExecutor
private static final Logger LOG
private static java.lang.Runnable NOOP
private final AtomicBiInteger _counts
private final java.util.concurrent.atomic.AtomicLong _lastShrink
private final java.util.Set<java.lang.Thread> _threads
private final java.lang.Object _joinLock
private final java.util.concurrent.BlockingQueue<java.lang.Runnable> _jobs
private final java.lang.ThreadGroup _threadGroup
private java.lang.String _name
private int _idleTimeout
private int _maxThreads
private int _minThreads
private int _reservedThreads
private TryExecutor _tryExecutor
private int _priority
private boolean _daemon
private boolean _detailedDump
private int _lowThreadsThreshold
private ThreadPoolBudget _budget
private final java.lang.Runnable _runnable
public QueuedThreadPool()
public QueuedThreadPool(@Name(value="maxThreads") int maxThreads, @Name(value="minThreads") int minThreads)
public QueuedThreadPool(@Name(value="maxThreads") int maxThreads, @Name(value="minThreads") int minThreads, @Name(value="queue") java.util.concurrent.BlockingQueue<java.lang.Runnable> queue)
public QueuedThreadPool(@Name(value="maxThreads") int maxThreads, @Name(value="minThreads") int minThreads, @Name(value="idleTimeout") int idleTimeout)
public QueuedThreadPool(@Name(value="maxThreads") int maxThreads, @Name(value="minThreads") int minThreads, @Name(value="idleTimeout") int idleTimeout, @Name(value="queue") java.util.concurrent.BlockingQueue<java.lang.Runnable> queue)
public QueuedThreadPool(@Name(value="maxThreads") int maxThreads, @Name(value="minThreads") int minThreads, @Name(value="idleTimeout") int idleTimeout, @Name(value="queue") java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, @Name(value="threadGroup") java.lang.ThreadGroup threadGroup)
public QueuedThreadPool(@Name(value="maxThreads") int maxThreads, @Name(value="minThreads") int minThreads, @Name(value="idleTimeout") int idleTimeout, @Name(value="reservedThreads") int reservedThreads, @Name(value="queue") java.util.concurrent.BlockingQueue<java.lang.Runnable> queue, @Name(value="threadGroup") java.lang.ThreadGroup threadGroup)
public ThreadPoolBudget getThreadPoolBudget()
getThreadPoolBudget
in interface ThreadPool.SizedThreadPool
public void setThreadPoolBudget(ThreadPoolBudget budget)
protected void doStart() throws java.lang.Exception
ContainerLifeCycle
doStart
in class ContainerLifeCycle
java.lang.Exception
protected void doStop() throws java.lang.Exception
ContainerLifeCycle
doStop
in class ContainerLifeCycle
java.lang.Exception
private void joinThreads(long stopByNanos) throws java.lang.InterruptedException
java.lang.InterruptedException
public void setDaemon(boolean daemon)
daemon
- true to enable delegationThread.setDaemon(boolean)
public void setIdleTimeout(int idleTimeout)
idleTimeout
- Max idle time in ms.getIdleTimeout()
public void setMaxThreads(int maxThreads)
setMaxThreads
in interface ThreadPool.SizedThreadPool
maxThreads
- maximum number of threads.getMaxThreads()
public void setMinThreads(int minThreads)
setMinThreads
in interface ThreadPool.SizedThreadPool
minThreads
- minimum number of threadsgetMinThreads()
public void setReservedThreads(int reservedThreads)
reservedThreads
- number of reserved threads or -1 for heuristically determinedgetReservedThreads()
public void setName(java.lang.String name)
name
- Name of this thread pool to use when naming threads.public void setThreadsPriority(int priority)
priority
- the new thread priority.@ManagedAttribute(value="maximum time a thread may be idle in ms") public int getIdleTimeout()
setIdleTimeout(int)
@ManagedAttribute(value="maximum number of threads in the pool") public int getMaxThreads()
getMaxThreads
in interface ThreadPool.SizedThreadPool
setMaxThreads(int)
@ManagedAttribute(value="minimum number of threads in the pool") public int getMinThreads()
getMinThreads
in interface ThreadPool.SizedThreadPool
setMinThreads(int)
@ManagedAttribute(value="the number of reserved threads in the pool") public int getReservedThreads()
setReservedThreads(int)
@ManagedAttribute(value="name of the thread pool") public java.lang.String getName()
@ManagedAttribute(value="priority of threads in the pool") public int getThreadsPriority()
@ManagedAttribute(value="size of the job queue") public int getQueueSize()
@ManagedAttribute(value="thread pool uses daemon threads") public boolean isDaemon()
Thread.setDaemon(boolean)
@ManagedAttribute(value="reports additional details in the dump") public boolean isDetailedDump()
public void setDetailedDump(boolean detailedDump)
@ManagedAttribute(value="threshold at which the pool is low on threads") public int getLowThreadsThreshold()
public void setLowThreadsThreshold(int lowThreadsThreshold)
public void execute(java.lang.Runnable job)
execute
in interface java.util.concurrent.Executor
execute
in interface TryExecutor
public boolean tryExecute(java.lang.Runnable task)
TryExecutor
tryExecute
in interface TryExecutor
task
- The task to be executedpublic void join() throws java.lang.InterruptedException
stopped
.join
in interface ThreadPool
java.lang.InterruptedException
- if thread was interrupted@ManagedAttribute(value="number of threads in the pool") public int getThreads()
getThreads
in interface ThreadPool
@ManagedAttribute(value="number of idle threads in the pool") public int getIdleThreads()
getIdleThreads
in interface ThreadPool
@ManagedAttribute(value="number of busy threads in the pool") public int getBusyThreads()
@ManagedAttribute(value="thread pool is low on threads", readonly=true) public boolean isLowOnThreads()
Returns whether this thread pool is low on threads.
The current formula is:
maxThreads - threads + idleThreads - queueSize <= lowThreadsThreshold
isLowOnThreads
in interface ThreadPool
getLowThreadsThreshold()
private void ensureThreads()
protected void startThread()
private boolean addCounts(int deltaThreads, int deltaIdle)
protected java.lang.Thread newThread(java.lang.Runnable runnable)
protected void removeThread(java.lang.Thread thread)
public void dump(java.lang.Appendable out, java.lang.String indent) throws java.io.IOException
Dumpable
dump
in interface Dumpable
dump
in class ContainerLifeCycle
out
- The appendable to dump toindent
- The indent to apply after any new lines.java.io.IOException
- if unable to write to Appendablepublic java.lang.String toString()
toString
in class AbstractLifeCycle
protected void runJob(java.lang.Runnable job)
Runs the given job in the current thread
.
Subclasses may override to perform pre/post actions before/after the job is run.
job
- the job to runprotected java.util.concurrent.BlockingQueue<java.lang.Runnable> getQueue()
@Deprecated public void setQueue(java.util.concurrent.BlockingQueue<java.lang.Runnable> queue)
queue
- the job queue@ManagedOperation(value="interrupts a pool thread") public boolean interruptThread(@Name(value="id") long id)
id
- the thread ID to interrupt.@ManagedOperation(value="dumps a pool thread stack") public java.lang.String dumpThread(@Name(value="id") long id)
id
- the thread ID to interrupt.