com.ibm.jusb.util
Class RunnableManager

java.lang.Object
  extended by com.ibm.jusb.util.RunnableManager
Direct Known Subclasses:
RunnableManager.SynchronizedRunnableManager

public class RunnableManager
extends java.lang.Object

Class to execute (and manage a Queue of) Runnables.

Runnables may be added to this at any time. They will not be run until this is started. When this is stopped Any Runnables previously added but not run will still be executed; the Thread that executes them (and the queue of the) are no longer associated with this; the RunnableManager's state is exactly as it was when initially created.

Note that this class is not (externally) Thread-safe; there is internal synchronization with the slave Thread, but no external synchronization. This should not be a problem if the max size is set high enough to never be reached. However, the default size is 1. To get an externally synchronized RunnableManager use the inner class SynchronizedRunnableManager.

Author:
Dan Streetman

Nested Class Summary
 class RunnableManager.SynchronizedRunnableManager
          An externally synchronized RunnableManager.
 
Field Summary
static long SIZE_UNLIMITED
           
 
Constructor Summary
RunnableManager()
          Constructor.
RunnableManager(boolean start)
          Constructor.
 
Method Summary
 void add(java.lang.Runnable newRunnable)
          Add a Runnable.
 long getMaxSize()
          Get the maximum number of Runnables to queue.
 java.lang.String getName()
          Get this RunnableManager's name.
 long getSize()
          Get the number of Runnables currently queued.
 boolean isRunning()
          If this is running.
 void setMaxSize(long size)
          Set the maximum number of Runnables to queue.
 void setName(java.lang.String n)
          Set this RunnableManager's name.
 void start()
          Start.
 void stop()
          Stop.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIZE_UNLIMITED

public static final long SIZE_UNLIMITED
See Also:
Constant Field Values
Constructor Detail

RunnableManager

public RunnableManager()
Constructor.

This will create a started RunnableManager.


RunnableManager

public RunnableManager(boolean start)
Constructor.

If start is false, this will create but not start a RunnableManager. If start is true, this is identical to the no-argument constructor.

Parameters:
start - if the new RunnableManager should be automatically started.
Method Detail

setName

public void setName(java.lang.String n)
Set this RunnableManager's name.

Parameters:
n - The new name to use.

getName

public java.lang.String getName()
Get this RunnableManager's name.

Returns:
The name.

add

public void add(java.lang.Runnable newRunnable)
Add a Runnable.

Parameters:
newRunnable - the Runnable to add.

start

public void start()
Start.

This must be started before any Runnables will be run. If currently running this will throw a IllegalThreadStateException.

Throws:
java.lang.IllegalThreadStateException - If this RunnableManager is already running.

stop

public void stop()
Stop.

This stops the currently running Thread. If not yet started this effectively abandons any Runnables previously added without executing them. If already started any Runnables added but not run will still be run; the Thread will exit after all have been run.

After calling stop the RunnableManager's state is effectively identical to when it was initially created (but stopped), i.e. Runnables may be added and the RunnableManager must be started.


isRunning

public boolean isRunning()
If this is running.

Returns:
if this is running.

getMaxSize

public long getMaxSize()
Get the maximum number of Runnables to queue.

If more than this many Runnables are queued, a new RunnableManager is created to handle further Runnables (and the old RunnableManager is abandoned, left to finish its Runnables, end its Thread, and eventually get garbage collected). The queue size is obviously reset, but the max is retained.

The default number is 1, meaning a new Thread is created for every Runnable added while another is in progress. This default allows complete isolation, i.e. no Runnable will be dependent on any other Runnable and no Runnable can starve any other. As long as Runnables execute reasonably quickly, most of the time the RunnableManager will be idle again by the time the next Runnable is added.

In any case, if this default proves to be too conservative for you, you can change it using setMaxSize().

Returns:
The maximum number of Runnables to queue.

setMaxSize

public void setMaxSize(long size)
Set the maximum number of Runnables to queue.

This sets the maximum number of Runnables that will be queued. See getMaxSize() for details.

Note that this is only effective for subsequently added Runnables; any Runnables already queued are not handled by a seperate RunnableManager. Also, this is only honored if the RunnableManager is started. Any Runnables added while the RunnableManager is stopped are executed by the same RunnableManager (once it is started).

Parameters:
size - The maximum number of Runnables to queue.
Throws:
java.lang.IllegalArgumentException - If the size is less than 1.

getSize

public long getSize()
Get the number of Runnables currently queued.

This returns the number of Runnables currently queued, including the one being processed.

Returns:
The number of Runnables queued.