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 Summary
Nested Classes Modifier and Type Class Description classThreadPool.ServiceThreadA service thread executes tasks.
-
Field Summary
Fields 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 Summary
Constructors 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 Summary
All 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_THREADS
public static final java.util.Map<java.lang.Thread,Future<?>> WAITING_THREADS
Threads waiting due to thread suspendable.
-
PROFILING
public static final boolean PROFILING
Enable call profiling.- See Also:
- Constant Field Values
-
PRINT_DELAY
public static final long PRINT_DELAY
Print every 10 seconds.- See Also:
- Constant Field Values
-
calls
protected static java.util.Map<java.lang.Class<?>,java.lang.Integer> calls
Service calls per runnable class.
-
threadcnt
protected static int threadcnt
The thread number.
-
poolcnt
protected static int poolcnt
The static thread pool number.
-
group
protected java.lang.ThreadGroup group
The thread group.
-
strategy
protected IPoolStrategy strategy
The strategy.
-
pool
protected java.util.List<ThreadPool.ServiceThread> pool
The pool of service threads.
-
parked
protected java.util.List<ThreadPool.ServiceThread> parked
The list of threads not used.
-
tasks
protected IBlockingQueue<java.lang.Runnable> tasks
The tasks to execute.
-
running
protected volatile boolean running
The running flag.
-
daemon
protected boolean daemon
The daemon flag.
-
enqueuetimes
protected java.util.Map<java.lang.Runnable,java.lang.Long> enqueuetimes
The current throughput of the pool (average of waiting times).
-
maxparked
protected int maxparked
The maximum number of parked threads.
-
timer
protected java.util.Timer timer
Rescue timer that checks if progress is made and tasks are scheduled.
-
maxwait
protected long maxwait
The time a task should maximum wait.
-
listeners
protected java.util.List<IChangeListener<java.lang.Void>> listeners
The finished listeners.
-
finished
protected boolean finished
Boolean if already finished.
-
-
Constructor Detail
-
ThreadPool
public ThreadPool()
Create a new thread pool.
-
ThreadPool
public ThreadPool(IPoolStrategy strategy)
Create a new thread pool.
-
ThreadPool
public ThreadPool(boolean daemon, IPoolStrategy strategy)Create a new thread pool.
-
ThreadPool
public ThreadPool(boolean daemon, IPoolStrategy strategy, long maxwait)Create a new thread pool.
-
-
Method Detail
-
isRunning
public boolean isRunning()
Test if the thread pool is running.- Specified by:
isRunningin interfaceIThreadPool
-
execute
public void execute(java.lang.Runnable task)
Execute a task in its own thread.- Specified by:
executein interfaceIThreadPool- Parameters:
task- The task to execute.
-
executeForever
public 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 interfaceIThreadPool- Parameters:
task- The task to execute.
-
dispose
public void dispose()
Shutdown the task pool- Specified by:
disposein interfaceIThreadPool
-
toString
public java.lang.String toString()
Get the string representation.- Overrides:
toStringin classjava.lang.Object- Returns:
- The string representation.
-
addThreads
protected void addThreads(int num)
Create some pool.- Parameters:
num- The number of pool.
-
addThread
protected void addThread()
-
getTask
protected java.lang.Runnable getTask(java.lang.Thread thread)
The task for a given thread.
-
addFinishListener
public void addFinishListener(IChangeListener<java.lang.Void> listener)
Add a finish listener;- Specified by:
addFinishListenerin interfaceIThreadPool
-
notifyFinishListeners
protected void notifyFinishListeners()
Notify the finish listeners.
-
main
public static void main(java.lang.String[] args)
Main for testing.- Parameters:
args- The arguments.
-
-