| java.lang.Object org.jgroups.util.ReusableThread
ReusableThread | public class ReusableThread implements Runnable(Code) | | 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
author: Bela Ban |
TASK_JOIN_TIME | final static long TASK_JOIN_TIME(Code) | | |
log | final protected static Log log(Code) | | |
suspended | volatile boolean suspended(Code) | | |
ReusableThread | public ReusableThread()(Code) | | |
ReusableThread | public ReusableThread(String thread_name)(Code) | | |
assignTask | public boolean assignTask(Runnable t)(Code) | | 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
|
assignThreadLocalListener | public void assignThreadLocalListener(ThreadLocalListener tl_listener)(Code) | | Assigns a ThreadLocalListener to the current ReusableThread. The ThreadLocalListener
sets ThreadLocal's values for the lifetime of the task for the thread that is used
to run the task. The ThreadLocalListener will reset values upon the task returning
and at this point will be deleted.
|
available | public boolean available()(Code) | | |
done | public boolean done()(Code) | | |
isAlive | public boolean isAlive()(Code) | | |
resume | public void resume()(Code) | | Resumes the thread. Noop if not suspended
|
run | public void run()(Code) | | 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()(Code) | | Will always be called from synchronized method, no need to do our own synchronization
|
stop | public void stop()(Code) | | 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()(Code) | | 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())
|
waitUntilDone | public void waitUntilDone()(Code) | | |
|
|