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