Package jadex.commons.concurrent
Class ThreadPool
- java.lang.Object
- 
- jadex.commons.concurrent.ThreadPool
 
- 
- All Implemented Interfaces:
- IThreadPool
 
 public class ThreadPool extends java.lang.Object implements IThreadPool A thread pool manages pool and saves resources and time by precreating and reusing pool.
- 
- 
Nested Class SummaryNested Classes Modifier and Type Class Description classThreadPool.ServiceThreadA service thread executes tasks.
 - 
Field SummaryFields Modifier and Type Field Description protected static java.util.Map<java.lang.Class<?>,java.lang.Integer>callsService calls per runnable class.protected booleandaemonThe daemon flag.protected java.util.Map<java.lang.Runnable,java.lang.Long>enqueuetimesThe current throughput of the pool (average of waiting times).protected booleanfinishedBoolean if already finished.protected java.lang.ThreadGroupgroupThe thread group.protected java.util.List<IChangeListener<java.lang.Void>>listenersThe finished listeners.protected intmaxparkedThe maximum number of parked threads.protected longmaxwaitThe time a task should maximum wait.protected java.util.List<ThreadPool.ServiceThread>parkedThe list of threads not used.protected java.util.List<ThreadPool.ServiceThread>poolThe pool of service threads.protected static intpoolcntThe static thread pool number.static longPRINT_DELAYPrint every 10 seconds.static booleanPROFILINGEnable call profiling.protected booleanrunningThe running flag.protected IPoolStrategystrategyThe strategy.protected IBlockingQueue<java.lang.Runnable>tasksThe tasks to execute.protected static intthreadcntThe thread number.protected java.util.TimertimerRescue timer that checks if progress is made and tasks are scheduled.static java.util.Map<java.lang.Thread,Future<?>>WAITING_THREADSThreads waiting due to thread suspendable.
 - 
Constructor SummaryConstructors Constructor Description ThreadPool()Create a new thread pool.ThreadPool(boolean daemon, IPoolStrategy strategy)Create a new thread pool.ThreadPool(boolean daemon, IPoolStrategy strategy, long maxwait)Create a new thread pool.ThreadPool(IPoolStrategy strategy)Create a new thread pool.
 - 
Method SummaryAll Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddFinishListener(IChangeListener<java.lang.Void> listener)Add a finish listener;protected voidaddThread()protected voidaddThreads(int num)Create some pool.voiddispose()Shutdown the task poolvoidexecute(java.lang.Runnable task)Execute a task in its own thread.voidexecuteForever(java.lang.Runnable task)Execute a task in its own thread.protected java.lang.RunnablegetTask(java.lang.Thread thread)The task for a given thread.booleanisRunning()Test if the thread pool is running.static voidmain(java.lang.String[] args)Main for testing.protected voidnotifyFinishListeners()Notify the finish listeners.java.lang.StringtoString()Get the string representation.
 
- 
- 
- 
Field Detail- 
WAITING_THREADSpublic static final java.util.Map<java.lang.Thread,Future<?>> WAITING_THREADS Threads waiting due to thread suspendable.
 - 
PROFILINGpublic static final boolean PROFILING Enable call profiling.- See Also:
- Constant Field Values
 
 - 
PRINT_DELAYpublic static final long PRINT_DELAY Print every 10 seconds.- See Also:
- Constant Field Values
 
 - 
callsprotected static java.util.Map<java.lang.Class<?>,java.lang.Integer> calls Service calls per runnable class.
 - 
threadcntprotected static int threadcnt The thread number.
 - 
poolcntprotected static int poolcnt The static thread pool number.
 - 
groupprotected java.lang.ThreadGroup group The thread group.
 - 
strategyprotected IPoolStrategy strategy The strategy.
 - 
poolprotected java.util.List<ThreadPool.ServiceThread> pool The pool of service threads.
 - 
parkedprotected java.util.List<ThreadPool.ServiceThread> parked The list of threads not used.
 - 
tasksprotected IBlockingQueue<java.lang.Runnable> tasks The tasks to execute.
 - 
runningprotected volatile boolean running The running flag.
 - 
daemonprotected boolean daemon The daemon flag.
 - 
enqueuetimesprotected java.util.Map<java.lang.Runnable,java.lang.Long> enqueuetimes The current throughput of the pool (average of waiting times).
 - 
maxparkedprotected int maxparked The maximum number of parked threads.
 - 
timerprotected java.util.Timer timer Rescue timer that checks if progress is made and tasks are scheduled.
 - 
maxwaitprotected long maxwait The time a task should maximum wait.
 - 
listenersprotected java.util.List<IChangeListener<java.lang.Void>> listeners The finished listeners.
 - 
finishedprotected boolean finished Boolean if already finished.
 
- 
 - 
Constructor Detail- 
ThreadPoolpublic ThreadPool() Create a new thread pool.
 - 
ThreadPoolpublic ThreadPool(IPoolStrategy strategy) Create a new thread pool.
 - 
ThreadPoolpublic ThreadPool(boolean daemon, IPoolStrategy strategy)Create a new thread pool.
 - 
ThreadPoolpublic ThreadPool(boolean daemon, IPoolStrategy strategy, long maxwait)Create a new thread pool.
 
- 
 - 
Method Detail- 
isRunningpublic boolean isRunning() Test if the thread pool is running.- Specified by:
- isRunningin interface- IThreadPool
 
 - 
executepublic void execute(java.lang.Runnable task) Execute a task in its own thread.- Specified by:
- executein interface- IThreadPool
- Parameters:
- task- The task to execute.
 
 - 
executeForeverpublic void executeForever(java.lang.Runnable task) Execute a task in its own thread. The pool expects the thread executing the task to never return. Preferably use this method if you want to permanently retrieve a thread e.g. for repeated blocking operations.- Specified by:
- executeForeverin interface- IThreadPool
- Parameters:
- task- The task to execute.
 
 - 
disposepublic void dispose() Shutdown the task pool- Specified by:
- disposein interface- IThreadPool
 
 - 
toStringpublic java.lang.String toString() Get the string representation.- Overrides:
- toStringin class- java.lang.Object
- Returns:
- The string representation.
 
 - 
addThreadsprotected void addThreads(int num) Create some pool.- Parameters:
- num- The number of pool.
 
 - 
addThreadprotected void addThread() 
 - 
getTaskprotected java.lang.Runnable getTask(java.lang.Thread thread) The task for a given thread.
 - 
addFinishListenerpublic void addFinishListener(IChangeListener<java.lang.Void> listener) Add a finish listener;- Specified by:
- addFinishListenerin interface- IThreadPool
 
 - 
notifyFinishListenersprotected void notifyFinishListeners() Notify the finish listeners.
 - 
mainpublic static void main(java.lang.String[] args) Main for testing.- Parameters:
- args- The arguments.
 
 
- 
 
-