Package org.jboss.util.threadpool
Class BasicThreadPool
- java.lang.Object
-
- org.jboss.util.threadpool.BasicThreadPool
-
- All Implemented Interfaces:
BasicThreadPoolMBean
,ThreadPool
,ThreadPoolMBean
public class BasicThreadPool extends java.lang.Object implements ThreadPool, BasicThreadPoolMBean
A basic thread pool. TODO: this port to jdk concurrent still needs to be tested.- Version:
- $Revision$
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
BasicThreadPool.RestoreTCCLThreadPoolExecutor
private class
BasicThreadPool.ThreadPoolThreadFactory
A factory for threadsprivate static class
BasicThreadPool.TimeoutInfo
An encapsulation of a task and its completion timeoutprivate class
BasicThreadPool.TimeoutMonitor
The monitor runnable which validates that threads are completing within the task completion timeout limits.
-
Field Summary
Fields Modifier and Type Field Description private BlockingMode
blockingMode
The blocking modeprivate ClassLoaderSource
classLoaderSource
Source for the thread contrext classloader to assign to threadsprivate ContextClassLoaderSwitcher
classLoaderSwitcher
private java.util.concurrent.ThreadPoolExecutor
executor
The pooled executorprivate static java.lang.ThreadGroup
JBOSS_THREAD_GROUP
The jboss thread groupprivate static java.util.concurrent.atomic.AtomicInteger
lastPoolNumber
The internal pool numberprivate java.util.concurrent.atomic.AtomicInteger
lastThreadNumber
The last thread numberprivate static org.jboss.logging.Logger
log
private java.lang.String
name
The thread pool nameprivate int
poolNumber
The internal pool numberprivate java.util.concurrent.LinkedBlockingQueue
queue
The queueprivate java.util.concurrent.atomic.AtomicBoolean
stopped
Has the pool been stopped?private java.util.concurrent.PriorityBlockingQueue<BasicThreadPool.TimeoutInfo>
tasksWithTimeouts
The Heapof tasks ordered by their completion timeout private java.lang.ThreadGroup
threadGroup
The thread groupprivate static java.util.Map
threadGroups
The thread groupsprivate BasicThreadPool.TimeoutMonitor
timeoutTask
The task completion timeout monitor runnableprivate boolean
trace
The trace level logging flag
-
Constructor Summary
Constructors Constructor Description BasicThreadPool()
Create a new thread poolBasicThreadPool(java.lang.String name)
Create a new thread pool with a default queue size of 1024, max pool size of 100, min pool size of 4, and a keep alive of 60 seconds.BasicThreadPool(java.lang.String name, java.lang.ThreadGroup threadGroup)
Create a new thread pool with a default queue size of 1024, max pool size of 100, min pool size of 4, and a keep alive of 60 seconds.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description protected void
checkTimeoutMonitor()
Used to lazily create the task completion timeout thread and monitorprotected void
execute(TaskWrapper wrapper)
Execute a taskprotected void
executeOnThread(TaskWrapper wrapper)
Execute a task on the same threadBlockingMode
getBlockingMode()
ClassLoaderSource
getClassLoaderSource()
Gets the source of the classloader that will be set as thethread context classloader
for pool threads.ThreadPool
getInstance()
long
getKeepAliveTime()
Get the keep alive timeint
getMaximumPoolSize()
Get the maximum pool sizeint
getMaximumQueueSize()
Get the maximum queue sizeint
getMinimumPoolSize()
Get the minimum pool sizejava.lang.String
getName()
Get the thread pool nameprotected BasicThreadPool.TimeoutInfo
getNextTimeout()
int
getPoolNumber()
Get the internal pool numberint
getPoolSize()
Get the current pool sizeint
getQueueSize()
Get the current queue sizejava.lang.ThreadGroup
getThreadGroup()
java.lang.String
getThreadGroupName()
Retrieve the thread group namevoid
run(java.lang.Runnable runnable)
Run a runnablevoid
run(java.lang.Runnable runnable, long startTimeout, long completeTimeout)
Run runnable with start and complete time out set explicitely.void
runTask(Task task)
Run a taskvoid
runTaskWrapper(TaskWrapper wrapper)
Run a task wrappervoid
setBlockingMode(java.lang.String name)
For backward compatibility with the previous string based modevoid
setBlockingMode(BlockingMode mode)
Set the behavior of the pool when a task is added and the queue is full.void
setBlockingModeString(java.lang.String name)
For backward compatibility with the previous string based mode This is needed for microcontainer as it gets confused with overloaded setters.void
setClassLoaderSource(ClassLoaderSource classLoaderSource)
Sets the source of the classloader that will be set as thethread context classloader
for pool threads.protected void
setDefaultThreadContextClassLoader(java.lang.Thread thread)
void
setKeepAliveTime(long time)
Set the keep alive timevoid
setMaximumPoolSize(int size)
Set the maximum pool sizevoid
setMaximumQueueSize(int size)
This resets the work queue capacity.void
setMinimumPoolSize(int size)
Set the minimum pool sizevoid
setName(java.lang.String name)
Set the thread pool namevoid
setThreadGroupName(java.lang.String threadGroupName)
Set the thread group namevoid
stop()
Stop the thread poolvoid
stop(boolean immediate)
Stop the pooljava.lang.String
toString()
protected void
waitForTask(TaskWrapper wrapper)
Wait for a taskvoid
waitForTasks()
Wait on the queued tasks to complete.void
waitForTasks(long maxWaitTime)
Wait on the queued tasks to complete upto maxWaitTime milliseconds.
-
-
-
Field Detail
-
JBOSS_THREAD_GROUP
private static final java.lang.ThreadGroup JBOSS_THREAD_GROUP
The jboss thread group
-
threadGroups
private static final java.util.Map threadGroups
The thread groups
-
lastPoolNumber
private static final java.util.concurrent.atomic.AtomicInteger lastPoolNumber
The internal pool number
-
log
private static org.jboss.logging.Logger log
-
name
private java.lang.String name
The thread pool name
-
poolNumber
private int poolNumber
The internal pool number
-
blockingMode
private BlockingMode blockingMode
The blocking mode
-
executor
private java.util.concurrent.ThreadPoolExecutor executor
The pooled executor
-
queue
private java.util.concurrent.LinkedBlockingQueue queue
The queue
-
threadGroup
private java.lang.ThreadGroup threadGroup
The thread group
-
classLoaderSource
private ClassLoaderSource classLoaderSource
Source for the thread contrext classloader to assign to threads
-
classLoaderSwitcher
private ContextClassLoaderSwitcher classLoaderSwitcher
-
lastThreadNumber
private java.util.concurrent.atomic.AtomicInteger lastThreadNumber
The last thread number
-
stopped
private java.util.concurrent.atomic.AtomicBoolean stopped
Has the pool been stopped?
-
tasksWithTimeouts
private java.util.concurrent.PriorityBlockingQueue<BasicThreadPool.TimeoutInfo> tasksWithTimeouts
The Heapof tasks ordered by their completion timeout
-
timeoutTask
private BasicThreadPool.TimeoutMonitor timeoutTask
The task completion timeout monitor runnable
-
trace
private boolean trace
The trace level logging flag
-
-
Constructor Detail
-
BasicThreadPool
public BasicThreadPool()
Create a new thread pool
-
BasicThreadPool
public BasicThreadPool(java.lang.String name)
Create a new thread pool with a default queue size of 1024, max pool size of 100, min pool size of 4, and a keep alive of 60 seconds.- Parameters:
name
- the pool name
-
BasicThreadPool
public BasicThreadPool(java.lang.String name, java.lang.ThreadGroup threadGroup)
Create a new thread pool with a default queue size of 1024, max pool size of 100, min pool size of 4, and a keep alive of 60 seconds.- Parameters:
name
- the pool namethreadGroup
- threadGroup
-
-
Method Detail
-
stop
public void stop(boolean immediate)
Description copied from interface:ThreadPool
Stop the pool- Specified by:
stop
in interfaceThreadPool
- Parameters:
immediate
- whether to shutdown immediately
-
waitForTasks
public void waitForTasks() throws java.lang.InterruptedException
Description copied from interface:ThreadPool
Wait on the queued tasks to complete. This can only be called after stop.- Specified by:
waitForTasks
in interfaceThreadPool
- Throws:
java.lang.InterruptedException
- for any iterruption error
-
waitForTasks
public void waitForTasks(long maxWaitTime) throws java.lang.InterruptedException
Description copied from interface:ThreadPool
Wait on the queued tasks to complete upto maxWaitTime milliseconds. This can only be called after stop.- Specified by:
waitForTasks
in interfaceThreadPool
- Parameters:
maxWaitTime
- the max wait time- Throws:
java.lang.InterruptedException
- for any interruption error
-
runTaskWrapper
public void runTaskWrapper(TaskWrapper wrapper)
Description copied from interface:ThreadPool
Run a task wrapper- Specified by:
runTaskWrapper
in interfaceThreadPool
- Parameters:
wrapper
- the task wrapper
-
runTask
public void runTask(Task task)
Description copied from interface:ThreadPool
Run a task- Specified by:
runTask
in interfaceThreadPool
- Parameters:
task
- the task
-
run
public void run(java.lang.Runnable runnable)
Description copied from interface:ThreadPool
Run a runnable- Specified by:
run
in interfaceThreadPool
- Parameters:
runnable
- the runnable
-
run
public void run(java.lang.Runnable runnable, long startTimeout, long completeTimeout)
Description copied from interface:ThreadPool
Run runnable with start and complete time out set explicitely.- Specified by:
run
in interfaceThreadPool
- Parameters:
runnable
- the runnablestartTimeout
- the start timeoutcompleteTimeout
- the complete timeout
-
getThreadGroup
public java.lang.ThreadGroup getThreadGroup()
-
getName
public java.lang.String getName()
Description copied from interface:ThreadPoolMBean
Get the thread pool name- Specified by:
getName
in interfaceThreadPoolMBean
- Returns:
- the thread pool name
-
setName
public void setName(java.lang.String name)
Description copied from interface:ThreadPoolMBean
Set the thread pool name- Specified by:
setName
in interfaceThreadPoolMBean
- Parameters:
name
- the name
-
getPoolNumber
public int getPoolNumber()
Description copied from interface:ThreadPoolMBean
Get the internal pool number- Specified by:
getPoolNumber
in interfaceThreadPoolMBean
- Returns:
- the internal pool number
-
getThreadGroupName
public java.lang.String getThreadGroupName()
Description copied from interface:BasicThreadPoolMBean
Retrieve the thread group name- Specified by:
getThreadGroupName
in interfaceBasicThreadPoolMBean
- Returns:
- the thread group name
-
setThreadGroupName
public void setThreadGroupName(java.lang.String threadGroupName)
Description copied from interface:BasicThreadPoolMBean
Set the thread group name- Specified by:
setThreadGroupName
in interfaceBasicThreadPoolMBean
- Parameters:
threadGroupName
- - the thread group name
-
getQueueSize
public int getQueueSize()
Description copied from interface:BasicThreadPoolMBean
Get the current queue size- Specified by:
getQueueSize
in interfaceBasicThreadPoolMBean
- Returns:
- the queue size
-
getMaximumQueueSize
public int getMaximumQueueSize()
Description copied from interface:BasicThreadPoolMBean
Get the maximum queue size- Specified by:
getMaximumQueueSize
in interfaceBasicThreadPoolMBean
- Returns:
- the maximum queue size
-
setMaximumQueueSize
public void setMaximumQueueSize(int size)
This resets the work queue capacity. This requires recreating the work queue and ThreadPoolExecutor, so this needs to be called before doing any work with the pool.- Specified by:
setMaximumQueueSize
in interfaceBasicThreadPoolMBean
- Parameters:
size
- new work queue capacity
-
getPoolSize
public int getPoolSize()
Description copied from interface:ThreadPoolMBean
Get the current pool size- Specified by:
getPoolSize
in interfaceThreadPoolMBean
- Returns:
- the current pool size
-
getMinimumPoolSize
public int getMinimumPoolSize()
Description copied from interface:ThreadPoolMBean
Get the minimum pool size- Specified by:
getMinimumPoolSize
in interfaceThreadPoolMBean
- Returns:
- the minimum pool size
-
setMinimumPoolSize
public void setMinimumPoolSize(int size)
Description copied from interface:ThreadPoolMBean
Set the minimum pool size- Specified by:
setMinimumPoolSize
in interfaceThreadPoolMBean
- Parameters:
size
- the minimum pool size
-
getMaximumPoolSize
public int getMaximumPoolSize()
Description copied from interface:ThreadPoolMBean
Get the maximum pool size- Specified by:
getMaximumPoolSize
in interfaceThreadPoolMBean
- Returns:
- the maximum pool size
-
setMaximumPoolSize
public void setMaximumPoolSize(int size)
Description copied from interface:ThreadPoolMBean
Set the maximum pool size- Specified by:
setMaximumPoolSize
in interfaceThreadPoolMBean
- Parameters:
size
- the maximum pool size
-
getKeepAliveTime
public long getKeepAliveTime()
Description copied from interface:BasicThreadPoolMBean
Get the keep alive time- Specified by:
getKeepAliveTime
in interfaceBasicThreadPoolMBean
- Returns:
- the keep alive time
-
setKeepAliveTime
public void setKeepAliveTime(long time)
Description copied from interface:BasicThreadPoolMBean
Set the keep alive time- Specified by:
setKeepAliveTime
in interfaceBasicThreadPoolMBean
- Parameters:
time
- the keep alive time
-
getBlockingMode
public BlockingMode getBlockingMode()
- Specified by:
getBlockingMode
in interfaceBasicThreadPoolMBean
- Returns:
- the blocking mode
-
setBlockingMode
public void setBlockingMode(BlockingMode mode)
Description copied from interface:BasicThreadPoolMBean
Set the behavior of the pool when a task is added and the queue is full. The mode string indicates one of the following modes: abort - a RuntimeException is thrown run - the calling thread executes the task wait - the calling thread blocks until the queue has room discard - the task is silently discarded without being run discardOldest - check to see if a task is about to complete and enque the new task if possible, else run the task in the calling thread- Specified by:
setBlockingMode
in interfaceBasicThreadPoolMBean
- Parameters:
mode
- one of run, wait, discard, discardOldest or abort without regard to case.
-
setBlockingMode
public void setBlockingMode(java.lang.String name)
For backward compatibility with the previous string based mode- Parameters:
name
- - the string form of the mode enum
-
setBlockingModeString
public void setBlockingModeString(java.lang.String name)
For backward compatibility with the previous string based mode This is needed for microcontainer as it gets confused with overloaded setters.- Parameters:
name
- - the string form of the mode enum
-
getClassLoaderSource
public ClassLoaderSource getClassLoaderSource()
Description copied from interface:BasicThreadPoolMBean
Gets the source of the classloader that will be set as thethread context classloader
for pool threads.- Specified by:
getClassLoaderSource
in interfaceBasicThreadPoolMBean
- Returns:
- the
ClassLoaderSource
. May returnnull
.
-
setClassLoaderSource
public void setClassLoaderSource(ClassLoaderSource classLoaderSource)
Description copied from interface:BasicThreadPoolMBean
Sets the source of the classloader that will be set as thethread context classloader
for pool threads. If set, whenever any new pool thread is created, it's context classloader will be set to the loader provided by this source. Further, when any thread is returned to the pool, its context classloader will be reset to the loader provided by this source.If set to
null
(the default), the pool will not attempt to manage the context classloader for pool threads; instead a newly created pool thread will inherit its context classloader from whatever thread triggered the addition to the pool. A thread returned to the pool will not have its context classloader changed from whatever it was.- Specified by:
setClassLoaderSource
in interfaceBasicThreadPoolMBean
- Parameters:
classLoaderSource
- theClassLoaderSource
. May benull
.
-
getInstance
public ThreadPool getInstance()
- Specified by:
getInstance
in interfaceThreadPoolMBean
- Returns:
- the instance
-
stop
public void stop()
Description copied from interface:ThreadPoolMBean
Stop the thread pool- Specified by:
stop
in interfaceThreadPoolMBean
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
executeOnThread
protected void executeOnThread(TaskWrapper wrapper)
Execute a task on the same thread- Parameters:
wrapper
- the task wrapper
-
execute
protected void execute(TaskWrapper wrapper)
Execute a task- Parameters:
wrapper
- the task wrapper
-
waitForTask
protected void waitForTask(TaskWrapper wrapper)
Wait for a task- Parameters:
wrapper
- the task wrapper
-
checkTimeoutMonitor
protected void checkTimeoutMonitor()
Used to lazily create the task completion timeout thread and monitor
-
getNextTimeout
protected BasicThreadPool.TimeoutInfo getNextTimeout()
-
setDefaultThreadContextClassLoader
protected void setDefaultThreadContextClassLoader(java.lang.Thread thread)
-
-