|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.ibm.jusb.util.RunnableManager
public class RunnableManager
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
.
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 |
---|
public static final long SIZE_UNLIMITED
Constructor Detail |
---|
public RunnableManager()
This will create a started RunnableManager.
public RunnableManager(boolean start)
If start is false, this will create but not start
a RunnableManager. If start is true, this is identical
to the no-argument constructor
.
start
- if the new RunnableManager should be automatically started.Method Detail |
---|
public void setName(java.lang.String n)
n
- The new name to use.public java.lang.String getName()
public void add(java.lang.Runnable newRunnable)
newRunnable
- the Runnable to add.public void start()
This must be started before any Runnables will be run.
If currently running
this will throw
a IllegalThreadStateException.
java.lang.IllegalThreadStateException
- If this RunnableManager is already running.public void 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.
public boolean isRunning()
public long getMaxSize()
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()
.
public void setMaxSize(long size)
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).
size
- The maximum number of Runnables to queue.
java.lang.IllegalArgumentException
- If the size is less than 1.public long getSize()
This returns the number of Runnables currently queued, including the one being processed.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |