org.jgroups.util

Class ReusableThread

public class ReusableThread extends 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:

  1. ReusableThread is started. Then, brefore assigning a task, it is stopped again
  2. ReusableThread is started, assigned a long running task. Then, before task is done, stop() is called
  3. ReusableThread is started, assigned a task. Then waitUntilDone() is called, then stop()
  4. ReusableThread is started, assigned a number of tasks (waitUntilDone() called between tasks), then stopped
  5. ReusableThread is started, assigned a task

Author: Bela Ban

Field Summary
protected static Loglog
Constructor Summary
ReusableThread()
ReusableThread(String thread_name)
Method Summary
booleanassignTask(Runnable t)
Assigns a task to the thread.
booleanavailable()
booleandone()
booleanisAlive()
voidresume()
Resumes the thread.
voidrun()
Delicate piece of code (means very important :-)).
voidstart()
Will always be called from synchronized method, no need to do our own synchronization
voidstop()
Stops the thread by setting thread=null and interrupting it.
voidsuspend()
Suspends the thread.
StringtoString()
voidwaitUntilDone()

Field Detail

log

protected static final Log log

Constructor Detail

ReusableThread

public ReusableThread()

ReusableThread

public ReusableThread(String thread_name)

Method Detail

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 ? 1998-2005 Bela Ban. All Rights Reserved.