org.apache.batik.util

Class RunnableQueue

public class RunnableQueue extends Object implements Runnable

This class represents an object which queues Runnable objects for invocation in a single thread.
Nested Class Summary
interfaceRunnableQueue.IdleRunnable
A {@link Runnable} that can also inform the caller how long it should be until it is run again.
protected static classRunnableQueue.Link
To store a Runnable.
protected static classRunnableQueue.LockableLink
To store a Runnable with an object waiting for him to be executed.
interfaceRunnableQueue.RunHandler
This interface must be implemented by an object which wants to be notified of run events.
static classRunnableQueue.RunHandlerAdapter
This is an adapter class that implements the RunHandler interface.
static classRunnableQueue.RunnableQueueState
Type-safe enumeration of queue states.
Field Summary
protected intpreemptCount
Count of preempt entries in queue, so preempt entries can be kept properly ordered.
protected RunnableQueue.RunHandlerrunHandler
The object which handle run events.
protected HaltingThreadrunnableQueueThread
The current thread.
static RunnableQueue.RunnableQueueStateRUNNING
The queue is in the process of running tasks.
protected RunnableQueue.RunnableQueueStatestate
The Suspension state of this thread.
protected ObjectstateLock
Object to synchronize/wait/notify for suspension issues.
static RunnableQueue.RunnableQueueStateSUSPENDED
The queue is no longer running any tasks and will not run any tasks until resumeExecution is called.
static RunnableQueue.RunnableQueueStateSUSPENDING
The queue may still be running tasks but as soon as possible will go to SUSPENDED state.
protected booleanwasResumed
Used to indicate if the queue was resumed while still running, so a 'resumed' event can be sent.
Method Summary
static RunnableQueuecreateRunnableQueue()
Creates a new RunnableQueue started in a new thread.
protected voidexecutionResumed()
Called when execution is being resumed.
protected voidexecutionSuspended()
Called when execution is being suspended.
ObjectgetIteratorLock()
Returns iterator lock to use to work with the iterator returned by iterator().
RunnableQueue.RunnableQueueStategetQueueState()
RunnableQueue.RunHandlergetRunHandler()
Returns the RunHandler or null.
HaltingThreadgetThread()
Returns the thread in which the RunnableQueue is currently running.
voidinvokeAndWait(Runnable r)
Waits until the given Runnable's run() has returned.
voidinvokeLater(Runnable r)
Schedules the given Runnable object for a later invocation, and returns.
Iteratoriterator()
Returns an iterator over the runnables.
voidpreemptAndWait(Runnable r)
Waits until the given Runnable's run() has returned.
voidpreemptLater(Runnable r)
Schedules the given Runnable object for a later invocation, and returns.
voidresumeExecution()
Resumes the execution of this queue.
voidrun()
Runs this queue.
protected voidrunnableInvoked(Runnable rable)
Called when a Runnable completes.
protected voidrunnableStart(Runnable rable)
Called just prior to executing a Runnable.
voidsetIdleRunnable(RunnableQueue.IdleRunnable r)
Sets a Runnable to be run whenever the queue is empty.
voidsetRunHandler(RunnableQueue.RunHandler rh)
Sets the RunHandler for this queue.
voidsuspendExecution(boolean waitTillSuspended)
Suspends the execution of this queue after the current runnable completes.

Field Detail

preemptCount

protected int preemptCount
Count of preempt entries in queue, so preempt entries can be kept properly ordered.

runHandler

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

runnableQueueThread

protected volatile HaltingThread runnableQueueThread
The current thread.

RUNNING

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

state

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

stateLock

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

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.

SUSPENDING

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

wasResumed

protected boolean wasResumed
Used to indicate if the queue was resumed while still running, so a 'resumed' event can be sent.

Method Detail

createRunnableQueue

public static RunnableQueue createRunnableQueue()
Creates a new RunnableQueue started in a new thread.

Returns: a RunnableQueue which is guaranteed to have entered its run() method.

executionResumed

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

executionSuspended

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

getIteratorLock

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

getQueueState

public RunnableQueue.RunnableQueueState getQueueState()

getRunHandler

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

getThread

public HaltingThread getThread()
Returns the thread in which the RunnableQueue is currently running.

Returns: null if the RunnableQueue has not entered his run() method.

invokeAndWait

public void invokeAndWait(Runnable r)
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: IllegalStateException if getThread() is null or if the thread returned by getThread() is the current one.

invokeLater

public void invokeLater(Runnable r)
Schedules the given Runnable object for a later invocation, and returns. An exception is thrown if the RunnableQueue was not started.

Throws: IllegalStateException if getThread() is null.

iterator

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

preemptAndWait

public void preemptAndWait(Runnable r)
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: IllegalStateException if getThread() is null or if the thread returned by getThread() is the current one.

preemptLater

public void preemptLater(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: IllegalStateException if getThread() is null.

resumeExecution

public void resumeExecution()
Resumes the execution of this queue.

Throws: IllegalStateException if getThread() is null.

run

public void run()
Runs this queue.

runnableInvoked

protected void runnableInvoked(Runnable rable)
Called when a Runnable completes. Currently just notifies runHandler

Parameters: rable The runnable that just completed.

runnableStart

protected void runnableStart(Runnable rable)
Called just prior to executing a Runnable. Currently just notifies runHandler

Parameters: rable The runnable that is about to start

setIdleRunnable

public void setIdleRunnable(RunnableQueue.IdleRunnable r)
Sets a Runnable to be run whenever the queue is empty.

setRunHandler

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

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: IllegalStateException if getThread() is null.

Copyright B) 2007 Apache Software Foundation. All Rights Reserved.