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 boolean
The daemon flag.The current throughput of the pool (average of waiting times).protected boolean
Boolean if already finished.protected ThreadGroup
The thread group.The finished listeners.protected int
The maximum number of parked threads.protected long
The 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 int
The static thread pool number.static final long
Print every 10 seconds.static final boolean
Enable call profiling.protected boolean
The running flag.protected jadex.common.IPoolStrategy
The strategy.protected jadex.collection.IBlockingQueue
<Runnable> The tasks to execute.protected static int
The thread number.protected Timer
Rescue 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 TypeMethodDescriptionvoid
addFinishListener
(jadex.common.IChangeListener<Void> listener) Add a finish listener;protected void
protected void
addThreads
(int num) Create some pool.void
dispose()
Shutdown the task poolvoid
Execute a task in its own thread.void
executeForever
(Runnable task) Execute a task in its own thread.protected Runnable
The task for a given thread.boolean
Test if the thread pool is running.static void
Main for testing.protected void
Notify 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:
isRunning
in interfaceIThreadPool
-
execute
Execute a task in its own thread.- Specified by:
execute
in 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:
executeForever
in interfaceIThreadPool
- Parameters:
task
- The task to execute.
-
dispose
public void dispose()Shutdown the task pool- Specified by:
dispose
in 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:
addFinishListener
in interfaceIThreadPool
-
notifyFinishListeners
protected void notifyFinishListeners()Notify the finish listeners. -
main
Main for testing.- Parameters:
args
- The arguments.
-