Package xal.tools.dispatch
Class DispatchQueue
java.lang.Object
xal.tools.dispatch.DispatchQueue
DispatchQueue which attempts to implement a subset of the open source
libdispatch library
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enum
possible states of the dispatch queue -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final int
priority for the background priority queuestatic final int
priority for the default priority queuestatic final int
priority for a high priority queuestatic final int
priority for the low priority queueprotected final ExecutorService
executor for processing dispatched operationsprotected final ThreadFactory
thread factory for dispatch this queueprotected final LinkedBlockingQueue<xal.tools.dispatch.DispatchOperation<?>>
queue of pending operations which have not yet been submitted for executionprotected final ExecutorService
executor which processes the queueprotected DispatchQueue.DispatchQueueState
state of this queueprotected final AtomicInteger
number of operations currently running -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
DispatchQueue
(String label) Constructorprotected
DispatchQueue
(String label, int priority) Primary Constructor -
Method Summary
Modifier and TypeMethodDescriptionstatic DispatchQueue
createConcurrentQueue
(String label) Create a concurrent queueprotected abstract ExecutorService
create the executor for dispatching operationsstatic DispatchQueue
createSerialQueue
(String label) Create a serial queueprotected int
decrement the running operations countvoid
dispatchAfter
(Date dispatchTime, Runnable rawOperation) dispatch the operation after the specified time without blockingvoid
dispatchAfterDelay
(long delay, Runnable rawOperation) Convenience method to dispatch the operation after the specified time delay in milliseconds from the current time.void
dispatchApply
(int iterations, DispatchIterationKernel iterationKernel) Performs all the specified iterations of the kernel asynchronously and waits for them to complete.void
dispatchAsync
(Runnable rawOperation) submit the operation for execution on the queue without waiting for completionvoid
dispatchAsync
(DispatchGroup group, Runnable rawOperation) submit the operation for execution on the queue and add it to the specified group without waiting for completionprotected void
dispatchAsync
(DispatchGroup group, Runnable rawOperation, boolean isBarrier) submit the operation for execution on the queue and add it to the specified group without waiting for completionvoid
dispatchBarrierAsync
(Runnable operation) Submit a barrier block for execution on the queue without waiting for completion.void
dispatchBarrierSync
(Runnable operation) Submit a barrier block for execution on the queue and wait for completion.void
dispatchSync
(Runnable rawOperation) submit the operation for execution on the queue and wait for it to completeprotected void
dispatchSync
(Runnable rawOperation, boolean isBarrier) submit the operation for execution on the queue and wait for it to complete<T> T
dispatchSync
(Callable<T> rawOperation) submit the operation for execution on the queue and wait for it to completevoid
dispose()
dispose of this queue - can only be called on a custom queueprotected <T> void
enqueueOperation
(xal.tools.dispatch.DispatchOperation<T> operation) Enqueue the operation and process make sure the operation queue gets processedprotected void
finalize()
dispose of the executorsstatic DispatchQueue
get the current queue or null if the current thread does not belong to a queuestatic DispatchQueue
Get the global background priority queuestatic DispatchQueue
Get the global default priority queuestatic DispatchQueue
Get the global high priority queuestatic DispatchQueue
Get the global low priority queuestatic DispatchQueue
getGlobalQueue
(int priority) Get the global queue corresponding to the specified priority.getLabel()
get this queue's labelstatic DispatchQueue
Get the main queue on which Swing events are dispatched.protected int
increment the running operations countboolean
Determine whether this queue is the current queueboolean
determine whether this queue has been disposedboolean
Determines whether this queue is suspended (disposed implies suspended)protected xal.tools.dispatch.DispatchOperation<Void>
makeDispatchOperation
(Runnable rawOperation) Make a callable operation wrapper from a raw runnable operationprotected xal.tools.dispatch.DispatchOperation<Void>
makeDispatchOperation
(Runnable rawOperation, boolean isBarrier) Make a callable operation wrapper from a raw runnable operationprotected <T> xal.tools.dispatch.DispatchOperation<T>
makeDispatchOperation
(Callable<T> rawOperation) Make a callable operation wrapper from a raw runnable operationprotected <T> xal.tools.dispatch.DispatchOperation<T>
makeDispatchOperation
(Callable<T> rawOperation, boolean isBarrier) Make a callable operation wrapper from a raw runnable operation<T> void
operationCompleted
(xal.tools.dispatch.DispatchOperation<T> operation) Event indicating that an operation in this group has completedprotected <T> void
postProcessOperation
(xal.tools.dispatch.DispatchOperation<T> operation) call this method when an operation has completed executionprotected abstract void
Process the operation queue by processing the next pending operation using the serial queue processor to guarantee the operations are queue seriallyprotected void
release allocated resources - called internally for any queuevoid
resume()
resume execution of pending operations if suspended or throw an exception if attempting to resume a disposed queuevoid
suspend()
suspend execution of pending operations if processing (nothing if disposed or already suspended)
-
Field Details
-
DISPATCH_QUEUE_PRIORITY_HIGH
public static final int DISPATCH_QUEUE_PRIORITY_HIGHpriority for a high priority queue -
DISPATCH_QUEUE_PRIORITY_DEFAULT
public static final int DISPATCH_QUEUE_PRIORITY_DEFAULTpriority for the default priority queue -
DISPATCH_QUEUE_PRIORITY_LOW
public static final int DISPATCH_QUEUE_PRIORITY_LOWpriority for the low priority queue -
DISPATCH_QUEUE_PRIORITY_BACKGROUND
public static final int DISPATCH_QUEUE_PRIORITY_BACKGROUNDpriority for the background priority queue -
queueProcessor
executor which processes the queue -
dispatchThreadFactory
thread factory for dispatch this queue -
dispatchExecutor
executor for processing dispatched operations -
runningOperationCounter
number of operations currently running -
queueState
state of this queue -
pendingOperationQueue
queue of pending operations which have not yet been submitted for execution
-
-
Constructor Details
-
DispatchQueue
Primary Constructor -
DispatchQueue
Constructor
-
-
Method Details
-
finalize
dispose of the executors -
getLabel
get this queue's label -
isSuspended
public boolean isSuspended()Determines whether this queue is suspended (disposed implies suspended) -
suspend
public void suspend()suspend execution of pending operations if processing (nothing if disposed or already suspended) -
resume
public void resume()resume execution of pending operations if suspended or throw an exception if attempting to resume a disposed queue -
dispose
public void dispose()dispose of this queue - can only be called on a custom queue -
isDisposed
public boolean isDisposed()determine whether this queue has been disposed -
releaseResources
protected void releaseResources()release allocated resources - called internally for any queue -
createDispatchExecutor
create the executor for dispatching operations -
createConcurrentQueue
Create a concurrent queue- Parameters:
label
- optional label for the queue to create- Returns:
- a new concurrent queue
-
createSerialQueue
Create a serial queue- Parameters:
label
- optional label for the queue to create- Returns:
- a new serial queue
-
getMainQueue
Get the main queue on which Swing events are dispatched. The main queue cannot be suspended or resumed. -
getGlobalQueue
Get the global queue corresponding to the specified priority. The global queue cannot be suspended or resumed.- Parameters:
priority
- one of DISPATCH_QUEUE_PRIORITY_HIGH, DISPATCH_QUEUE_PRIORITY_DEFAULT, DISPATCH_QUEUE_PRIORITY_LOW, DISPATCH_QUEUE_PRIORITY_BACKGROUND- Returns:
- the global queue corresponding to the specified priority or null if none exists.
-
getGlobalDefaultPriorityQueue
Get the global default priority queue -
getGlobalHighPriorityQueue
Get the global high priority queue -
getGlobalLowPriorityQueue
Get the global low priority queue -
getGlobalBackgroundPriorityQueue
Get the global background priority queue -
getCurrentQueue
get the current queue or null if the current thread does not belong to a queue -
isCurrentQueue
public boolean isCurrentQueue()Determine whether this queue is the current queue -
dispatchSync
submit the operation for execution on the queue and wait for it to complete -
dispatchSync
submit the operation for execution on the queue and wait for it to complete -
dispatchSync
submit the operation for execution on the queue and wait for it to complete -
dispatchAsync
submit the operation for execution on the queue without waiting for completion -
dispatchAsync
submit the operation for execution on the queue and add it to the specified group without waiting for completion -
dispatchAsync
submit the operation for execution on the queue and add it to the specified group without waiting for completion -
dispatchAfterDelay
Convenience method to dispatch the operation after the specified time delay in milliseconds from the current time.- Parameters:
delay
- Time delay in milliseconds from the current time after which the operation should runrawOperation
- the operation to run
-
dispatchAfter
dispatch the operation after the specified time without blocking -
dispatchBarrierAsync
Submit a barrier block for execution on the queue without waiting for completion. The barrier waits for all operations on the queue to complete before executing and then blocks all other threads on the concurrent queue until it completes. Only relevant on a concurrent queue created using createConcurrentQueue(). On all other queues, it is equivalent to dispatchAsync().- Parameters:
operation
- the operation to execute
-
dispatchBarrierSync
Submit a barrier block for execution on the queue and wait for completion. The barrier waits for all operations on the queue to complete before executing and then blocks all other threads on the concurrent queue until it completes. Only relevant on a concurrent queue created using createConcurrentQueue(). On all other queues, it is equivalent to dispatchSync().- Parameters:
operation
- the operation to execute
-
dispatchApply
Performs all the specified iterations of the kernel asynchronously and waits for them to complete. -
enqueueOperation
protected <T> void enqueueOperation(xal.tools.dispatch.DispatchOperation<T> operation) Enqueue the operation and process make sure the operation queue gets processed -
processOperationQueue
protected abstract void processOperationQueue()Process the operation queue by processing the next pending operation using the serial queue processor to guarantee the operations are queue serially -
postProcessOperation
protected <T> void postProcessOperation(xal.tools.dispatch.DispatchOperation<T> operation) call this method when an operation has completed execution -
makeDispatchOperation
protected <T> xal.tools.dispatch.DispatchOperation<T> makeDispatchOperation(Callable<T> rawOperation) Make a callable operation wrapper from a raw runnable operation -
makeDispatchOperation
Make a callable operation wrapper from a raw runnable operation -
makeDispatchOperation
protected xal.tools.dispatch.DispatchOperation<Void> makeDispatchOperation(Runnable rawOperation, boolean isBarrier) Make a callable operation wrapper from a raw runnable operation -
makeDispatchOperation
protected <T> xal.tools.dispatch.DispatchOperation<T> makeDispatchOperation(Callable<T> rawOperation, boolean isBarrier) Make a callable operation wrapper from a raw runnable operation -
operationCompleted
public <T> void operationCompleted(xal.tools.dispatch.DispatchOperation<T> operation) Event indicating that an operation in this group has completed -
incrementRunningOperationCount
protected int incrementRunningOperationCount()increment the running operations count -
decrementRunningOperationCount
protected int decrementRunningOperationCount()decrement the running operations count
-