Class ReusableThread

java.lang.Object
org.jgroups.util.ReusableThread
All Implemented Interfaces:
Runnable

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 Details

    • log

      protected static final org.apache.commons.logging.Log log
  • Constructor Details

    • ReusableThread

      public ReusableThread()
    • ReusableThread

      public ReusableThread(String thread_name)
  • Method Details

    • 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(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 interface Runnable
    • waitUntilDone

      public void waitUntilDone()
    • toString

      public String toString()
      Overrides:
      toString in class Object