Package jadex.concurrent
Class ThreadPool
java.lang.Object
jadex.concurrent.ThreadPool
- All Implemented Interfaces:
IThreadPool
A thread pool manages pool and saves resources
and time by precreating and reusing pool.
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescriptionService calls per runnable class.protected booleanThe daemon flag.The current throughput of the pool (average of waiting times).protected booleanBoolean if already finished.protected ThreadGroupThe thread group.The finished listeners.protected intThe maximum number of parked threads.protected longThe time a task should maximum wait.protected List<ThreadPool.ServiceThread> The list of threads not used.protected List<ThreadPool.ServiceThread> The pool of service threads.protected static intThe static thread pool number.static final longPrint every 10 seconds.static final booleanEnable call profiling.protected booleanThe running flag.protected jadex.common.IPoolStrategyThe strategy.protected jadex.collection.IBlockingQueue<Runnable> The tasks to execute.protected static intThe thread number.protected TimerRescue timer that checks if progress is made and tasks are scheduled. -
Constructor Summary
ConstructorsConstructorDescriptionCreate a new thread pool.ThreadPool(boolean daemon, jadex.common.IPoolStrategy strategy) Create a new thread pool.ThreadPool(boolean daemon, jadex.common.IPoolStrategy strategy, long maxwait) Create a new thread pool.ThreadPool(jadex.common.IPoolStrategy strategy) Create a new thread pool. -
Method Summary
Modifier and TypeMethodDescriptionvoidaddFinishListener(jadex.common.IChangeListener<Void> listener) Add a finish listener;protected voidprotected voidaddThreads(int num) Create some pool.voiddispose()Shutdown the task poolvoidExecute a task in its own thread.voidexecuteForever(Runnable task) Execute a task in its own thread.protected RunnableThe task for a given thread.booleanTest if the thread pool is running.static voidMain for testing.protected voidNotify the finish listeners.toString()Get the string representation.
-
Field Details
-
PROFILING
public static final boolean PROFILINGEnable call profiling.- See Also:
-
PRINT_DELAY
public static final long PRINT_DELAYPrint every 10 seconds.- See Also:
-
calls
Service calls per runnable class. -
threadcnt
protected static int threadcntThe thread number. -
poolcnt
protected static int poolcntThe static thread pool number. -
group
The thread group. -
strategy
protected jadex.common.IPoolStrategy strategyThe strategy. -
pool
The pool of service threads. -
parked
The list of threads not used. -
tasks
The tasks to execute. -
running
protected volatile boolean runningThe running flag. -
daemon
protected boolean daemonThe daemon flag. -
enqueuetimes
The current throughput of the pool (average of waiting times). -
maxparked
protected int maxparkedThe maximum number of parked threads. -
timer
Rescue timer that checks if progress is made and tasks are scheduled. -
maxwait
protected long maxwaitThe time a task should maximum wait. -
listeners
The finished listeners. -
finished
protected boolean finishedBoolean if already finished.
-
-
Constructor Details
-
ThreadPool
public ThreadPool()Create a new thread pool. -
ThreadPool
public ThreadPool(jadex.common.IPoolStrategy strategy) Create a new thread pool. -
ThreadPool
public ThreadPool(boolean daemon, jadex.common.IPoolStrategy strategy) Create a new thread pool. -
ThreadPool
public ThreadPool(boolean daemon, jadex.common.IPoolStrategy strategy, long maxwait) Create a new thread pool.
-
-
Method Details
-
isRunning
public boolean isRunning()Test if the thread pool is running.- Specified by:
isRunningin interfaceIThreadPool
-
execute
Execute a task in its own thread.- Specified by:
executein interfaceIThreadPool- Parameters:
task- The task to execute.
-
executeForever
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
Get the string representation. -
addThreads
protected void addThreads(int num) Create some pool.- Parameters:
num- The number of pool.
-
addThread
protected void addThread() -
getTask
The task for a given thread. -
addFinishListener
Add a finish listener;- Specified by:
addFinishListenerin interfaceIThreadPool
-
notifyFinishListeners
protected void notifyFinishListeners()Notify the finish listeners. -
main
Main for testing.- Parameters:
args- The arguments.
-