org.apache.batik.util
Class RunnableQueue

java.lang.Object
  |
  +--org.apache.batik.util.RunnableQueue
All Implemented Interfaces:
java.lang.Runnable

public class RunnableQueue
extends java.lang.Object
implements java.lang.Runnable

This class represents an object which queues Runnable objects for invocation in a single thread.


Inner Class Summary
protected static class RunnableQueue.Link
          To store a Runnable.
protected static class RunnableQueue.LockableLink
          To store a Runnable with an object waiting for him to be executed.
static interface RunnableQueue.RunHandler
          This interface must be implemented by an object which wants to be notified of run events.
static class RunnableQueue.RunnableQueueState
          Type-safe enumeration of queue states.
 
Field Summary
protected  DoublyLinkedList list
          The Runnable objects list, also used as synchoronization point for pushing/poping runables.
protected  RunnableQueue.RunHandler runHandler
          The object which handle run events.
protected  java.lang.Thread runnableQueueThread
          The current thread.
static RunnableQueue.RunnableQueueState RUNNING
          The queue is in the processes of running tasks.
protected  RunnableQueue.RunnableQueueState state
          The Suspension state of this thread.
protected  java.lang.Object stateLock
          Object to synchronize/wait/notify for suspension issues.
static RunnableQueue.RunnableQueueState SUSPENDED
          The queue is no longer running any tasks and will not run any tasks until resumeExecution is called.
static RunnableQueue.RunnableQueueState SUSPENDING
          The queue may still be running tasks but as soon as possible will go to SUSPENDED state.
 
Constructor Summary
RunnableQueue()
           
 
Method Summary
static RunnableQueue createRunnableQueue()
          Creates a new RunnableQueue started in a new thread.
protected  void executionResumed()
          Called when execution is being resumed.
protected  void executionSuspended()
          Called when execution is being suspended.
 java.lang.Object getIteratorLock()
          Returns iterator lock to use to work with the iterator returned by iterator().
 RunnableQueue.RunnableQueueState getQueueState()
           
 RunnableQueue.RunHandler getRunHandler()
          Returns the RunHandler or null.
 java.lang.Thread getThread()
          Returns the thread in which the RunnableQueue is currently running.
 void invokeAndWait(java.lang.Runnable r)
          Waits until the given Runnable's run() has returned.
 void invokeLater(java.lang.Runnable r)
          Schedules the given Runnable object for a later invocation, and returns.
 java.util.Iterator iterator()
          Returns an iterator over the runnables.
 void preemptAndWait(java.lang.Runnable r)
          Waits until the given Runnable's run() has returned.
 void preemptLater(java.lang.Runnable r)
          Schedules the given Runnable object for a later invocation, and returns.
 void resumeExecution()
          Resumes the execution of this queue.
 void run()
          Runs this queue.
protected  void runnableInvoked(java.lang.Runnable rable)
          Called when a Runnable completes.
 void setRunHandler(RunnableQueue.RunHandler rh)
          Sets the RunHandler for this queue.
 void suspendExecution(boolean waitTillSuspended)
          Suspends the execution of this queue after the current runnable completes.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

RUNNING

public static final RunnableQueue.RunnableQueueState RUNNING
The queue is in the processes of running tasks.

SUSPENDING

public static final RunnableQueue.RunnableQueueState SUSPENDING
The queue may still be running tasks but as soon as possible will go to SUSPENDED state.

SUSPENDED

public static final RunnableQueue.RunnableQueueState SUSPENDED
The queue is no longer running any tasks and will not run any tasks until resumeExecution is called.

state

protected RunnableQueue.RunnableQueueState state
The Suspension state of this thread.

stateLock

protected java.lang.Object stateLock
Object to synchronize/wait/notify for suspension issues.

list

protected DoublyLinkedList list
The Runnable objects list, also used as synchoronization point for pushing/poping runables.

runHandler

protected RunnableQueue.RunHandler runHandler
The object which handle run events.

runnableQueueThread

protected java.lang.Thread runnableQueueThread
The current thread.
Constructor Detail

RunnableQueue

public RunnableQueue()
Method Detail

createRunnableQueue

public static RunnableQueue createRunnableQueue()
Creates a new RunnableQueue started in a new thread.
Returns:
a RunnableQueue which is garanteed to have entered its run() method.

run

public void run()
Runs this queue.
Specified by:
run in interface java.lang.Runnable

getThread

public java.lang.Thread getThread()
Returns the thread in which the RunnableQueue is currently running.
Returns:
null if the RunnableQueue has not entered his run() method.

invokeLater

public void invokeLater(java.lang.Runnable r)
Schedules the given Runnable object for a later invocation, and returns. An exception is thrown if the RunnableQueue was not started.
Throws:
java.lang.IllegalStateException - if getThread() is null.

invokeAndWait

public void invokeAndWait(java.lang.Runnable r)
                   throws java.lang.InterruptedException
Waits until the given Runnable's run() has returned. Note: invokeAndWait() must not be called from the current thread (for example from the run() method of the argument).
Throws:
java.lang.IllegalStateException - if getThread() is null or if the thread returned by getThread() is the current one.

preemptLater

public void preemptLater(java.lang.Runnable r)
Schedules the given Runnable object for a later invocation, and returns. The given runnable preempts any runnable that is not currently executing (ie the next runnable started will be the one given). An exception is thrown if the RunnableQueue was not started.
Throws:
java.lang.IllegalStateException - if getThread() is null.

preemptAndWait

public void preemptAndWait(java.lang.Runnable r)
                    throws java.lang.InterruptedException
Waits until the given Runnable's run() has returned. The given runnable preempts any runnable that is not currently executing (ie the next runnable started will be the one given). Note: preemptAndWait() must not be called from the current thread (for example from the run() method of the argument).
Throws:
java.lang.IllegalStateException - if getThread() is null or if the thread returned by getThread() is the current one.

getQueueState

public RunnableQueue.RunnableQueueState getQueueState()

suspendExecution

public void suspendExecution(boolean waitTillSuspended)
Suspends the execution of this queue after the current runnable completes.
Parameters:
waitTillSuspended - if true this method will not return until the queue has suspended (no runnable in progress or about to be in progress). If resumeExecution is called while waiting will simply return (this really indicates a race condition in your code). This may return before an associated RunHandler is notified.
Throws:
java.lang.IllegalStateException - if getThread() is null.

resumeExecution

public void resumeExecution()
Resumes the execution of this queue.
Throws:
java.lang.IllegalStateException - if getThread() is null.

getIteratorLock

public java.lang.Object getIteratorLock()
Returns iterator lock to use to work with the iterator returned by iterator().

iterator

public java.util.Iterator iterator()
Returns an iterator over the runnables.

setRunHandler

public void setRunHandler(RunnableQueue.RunHandler rh)
Sets the RunHandler for this queue.

getRunHandler

public RunnableQueue.RunHandler getRunHandler()
Returns the RunHandler or null.

executionSuspended

protected void executionSuspended()
Called when execution is being suspended. Currently just notifies runHandler

executionResumed

protected void executionResumed()
Called when execution is being resumed. Currently just notifies runHandler

runnableInvoked

protected void runnableInvoked(java.lang.Runnable rable)
Called when a Runnable completes. Currently just notifies runHandler
Parameters:
rable - The runnable that just completed.


Copyright © 2002 Apache Software Foundation. All Rights Reserved.