Package org.jgroups.util
Class ReusableThread
- java.lang.Object
-
- org.jgroups.util.ReusableThread
-
- All Implemented Interfaces:
java.lang.Runnable
public class ReusableThread extends java.lang.Object implements java.lang.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 whoserun
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
-
-
Field Summary
Fields Modifier and Type Field Description protected static org.apache.commons.logging.Log
log
-
Constructor Summary
Constructors Constructor Description ReusableThread()
ReusableThread(java.lang.String thread_name)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description boolean
assignTask(java.lang.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 synchronizationvoid
stop()
Stops the thread by setting thread=null and interrupting it.void
suspend()
Suspends the thread.java.lang.String
toString()
void
waitUntilDone()
-
-
-
Method Detail
-
done
public boolean done()
-
available
public boolean available()
-
isAlive
public boolean isAlive()
-
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())
-
resume
public void resume()
Resumes the thread. Noop if not suspended
-
assignTask
public boolean assignTask(java.lang.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
-
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.- Specified by:
run
in interfacejava.lang.Runnable
-
waitUntilDone
public void waitUntilDone()
-
toString
public java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
-