|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--java.lang.ThreadGroup | +--com.ls.util.threading.ThreadPool
This class provides a pool of threads. The threads can be usesd for executing so-called "tasks". A task is a
generic piece of code modelled as a class which implement the Runnable
interface. Instances of such a
class can be passed to this thread pool by calling runTask(Runnable)
. The pool dynamically
allocates an idle thread for execution. The allocated thread executes the task by calling the
Runnable.run()
method.
Constructor Summary | |
ThreadPool(String name)
Constructs a new thread pool with the given name. |
Method Summary | |
void |
join()
Waits until all threads in this thread pool are terminated. |
static ThreadPool |
newInstance(Properties props)
Factory method that instaniates a new thread pool implementation according to the properties defined by the given 'props' argument. |
abstract void |
runTask(Runnable task)
Executes the given task by a pool thread. |
void |
setLogger(ILogger logger)
Sets the logging interface that should be used by this thread pool to report diagnostic messages. |
protected ThreadPoolWorker |
startNewWorker()
Starts a new worker thread and adds it to this thread pool. |
protected ThreadPoolWorker[] |
startNewWorker(int amount)
Performs the same task as startNewWorker() as often as specified by the 'amount' argument. |
protected void |
threadBusy(ThreadPoolWorker worker,
Runnable task)
Called by a worker thread to notify the thread pool, that the worker is going to execute the given task. |
protected void |
threadInitialize(ThreadPoolWorker worker)
Called by a worker thread to notify the thread pool, that the worker thread is initializing. |
protected void |
threadReady(ThreadPoolWorker worker)
Called by a worker thread to notify the thread pool, that the worker thread is ready to process a task. |
protected void |
threadShutdown(ThreadPoolWorker worker)
Called by a worker thread to notify the thread pool that the worker will shut down. |
protected void |
threadTaskCompleted(ThreadPoolWorker worker,
Runnable task)
Called by a worker thread to inform the thread pool about successfull task execution. |
protected void |
threadTaskFailed(ThreadPoolWorker worker,
Runnable task,
Throwable error)
Called by a worker thread to inform the thread pool about failure of task execution. |
void |
uncaughtException(Thread thread,
Throwable thr)
Called by the worker threads if an unexpected exception was raised in the threads ThreadPoolWorker.run()
method. |
abstract void |
waitForIdlePool()
Waits until all threads are ready for task execution, or in other words: waits until all currently processed tasks are finished. |
abstract void |
waitForIdleThread()
Waites until a thread in the pool becomes ready for task execution. |
Methods inherited from class java.lang.ThreadGroup |
activeCount, activeGroupCount, allowThreadSuspension, checkAccess, destroy, enumerate, enumerate, enumerate, enumerate, getMaxPriority, getName, getParent, interrupt, isDaemon, isDestroyed, list, parentOf, resume, setDaemon, setMaxPriority, stop, suspend, toString |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Constructor Detail |
public ThreadPool(String name)
name
- The name of this thread pool.Method Detail |
public static ThreadPool newInstance(Properties props) throws BuildException
props
- Properties required to instanatiate a new thread pool.BuildException
- If the build failed due to invalid property values or reflection errors.public void setLogger(ILogger logger)
null
as argument will result in a NullPointerException later on.logger
- the logging interface which should be used for reportingpublic abstract void waitForIdleThread() throws InterruptedException
InterruptedException
- If the caller thread gets interrupted while waiting.public abstract void waitForIdlePool() throws InterruptedException
InterruptedException
- If the caller thread gets interrupted while waiting. *public abstract void runTask(Runnable task) throws InterruptedException
task
- task to runInterruptedException
- If the caller thread gets interrupted while waiting.public void join() throws InterruptedException
InterruptedException
- If the caller thread gets interrupted while waiting.public void uncaughtException(Thread thread, Throwable thr)
ThreadPoolWorker.run()
method. By default this method reports the exception to an ILogger
interface, which can be set by
calling method setLogger(ILogger)
.uncaughtException
in class ThreadGroup
thread
- The thread that caused the exception.thr
- The exception that has raised.protected ThreadPoolWorker startNewWorker()
protected ThreadPoolWorker[] startNewWorker(int amount)
startNewWorker()
as often as specified by the 'amount' argument. Returns all
created worker threads in an array.amount
- The amount of worker threads, that should be created and started at once.protected void threadInitialize(ThreadPoolWorker worker)
worker
- The worker thread that is initializing.protected void threadReady(ThreadPoolWorker worker)
ThreadPoolWorker.runTask(Runnable)
to assign the next
pending task to the worker thread, for instance.worker
- The worker thread that is ready.protected void threadBusy(ThreadPoolWorker worker, Runnable task)
worker
- The worker that will be busy because of task execution.task
- The task that will be executed by the worker.protected void threadTaskCompleted(ThreadPoolWorker worker, Runnable task)
worker
- The worker that has just successfully completed a task.task
- The task that succeeded.protected void threadTaskFailed(ThreadPoolWorker worker, Runnable task, Throwable error)
worker
- The worker that has failed to execute a task.task
- The task that failed.error
- The specific exception that was raised by the task.protected void threadShutdown(ThreadPoolWorker worker)
worker
- The worker thread that will perform a shutdown.
|
|||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||
SUMMARY: INNER | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |