org.jgroups.util
Class ReusableThread
java.lang.Object
org.jgroups.util.ReusableThread
- Runnable
public class ReusableThread
extends java.lang.Object
implements Runnable
Reusable thread class. Instead of creating a new thread per task, this instance can be reused
to run different tasks in turn. This is done by looping and assigning the Runnable task objects
whose
run
method is then called.
Tasks are Runnable objects and should be prepared to terminate when they receive an
InterruptedException. This is thrown by the stop() method.
The following situations have to be tested:
- ReusableThread is started. Then, brefore assigning a task, it is stopped again
- ReusableThread is started, assigned a long running task. Then, before task is done,
stop() is called
- ReusableThread is started, assigned a task. Then waitUntilDone() is called, then stop()
- ReusableThread is started, assigned a number of tasks (waitUntilDone() called between tasks),
then stopped
- ReusableThread is started, assigned a task
boolean | assignTask(Runnable t) - Assigns a task to the thread.
|
boolean | available()
|
boolean | done()
|
boolean | isAlive()
|
void | resume() - Resumes the thread.
|
void | run() - Delicate piece of code (means very important :-)).
|
void | start() - Will always be called from synchronized method, no need to do our own synchronization
|
void | stop() - Stops the thread by setting thread=null and interrupting it.
|
void | suspend() - Suspends the thread.
|
String | toString()
|
void | waitUntilDone()
|
log
protected static final Log log
ReusableThread
public ReusableThread()
ReusableThread
public ReusableThread(String thread_name)
assignTask
public boolean assignTask(Runnable t)
Assigns a task to the thread. If the thread is not running, it will be started. It it is
already working on a task, it will reject the new task. Returns true if task could be
assigned auccessfully
available
public boolean available()
done
public boolean done()
isAlive
public boolean isAlive()
resume
public void resume()
Resumes the thread. Noop if not suspended
run
public void run()
Delicate piece of code (means very important :-)). Works as follows: loops until stop is true.
Waits in a loop until task is assigned. Then runs the task and notifies waiters that it's done
when task is completed. Then returns to the first loop to wait for more work. Does so until
stop() is called, which sets stop=true and interrupts the thread. If waiting for a task, the
thread terminates. If running a task, the task is interrupted, and the thread terminates. If the
task is not interrupible, the stop() method will wait for 3 secs (join on the thread), then return.
This means that the run() method of the task will complete and only then will the thread be
garbage-collected.
start
public void start()
Will always be called from synchronized method, no need to do our own synchronization
stop
public void stop()
Stops the thread by setting thread=null and interrupting it. The run() method catches the
InterruptedException and checks whether thread==null. If this is the case, it will terminate
suspend
public void suspend()
Suspends the thread. Does nothing if already suspended. If a thread is waiting to be assigned a task, or
is currently running a (possibly long-running) task, then it will be suspended the next time it
waits for suspended==false (second wait-loop in run())
toString
public String toString()
waitUntilDone
public void waitUntilDone()
Copyright B) 1998-2005 Bela Ban. All Rights Reserved.