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 enumpossible states of the dispatch queue -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intpriority for the background priority queuestatic final intpriority for the default priority queuestatic final intpriority for a high priority queuestatic final intpriority for the low priority queueprotected final ExecutorServiceexecutor for processing dispatched operationsprotected final ThreadFactorythread factory for dispatch this queueprotected final LinkedBlockingQueue<xal.tools.dispatch.DispatchOperation<?>>queue of pending operations which have not yet been submitted for executionprotected final ExecutorServiceexecutor which processes the queueprotected DispatchQueue.DispatchQueueStatestate of this queueprotected final AtomicIntegernumber of operations currently running -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDispatchQueue(String label) ConstructorprotectedDispatchQueue(String label, int priority) Primary Constructor -
Method Summary
Modifier and TypeMethodDescriptionstatic DispatchQueuecreateConcurrentQueue(String label) Create a concurrent queueprotected abstract ExecutorServicecreate the executor for dispatching operationsstatic DispatchQueuecreateSerialQueue(String label) Create a serial queueprotected intdecrement the running operations countvoiddispatchAfter(Date dispatchTime, Runnable rawOperation) dispatch the operation after the specified time without blockingvoiddispatchAfterDelay(long delay, Runnable rawOperation) Convenience method to dispatch the operation after the specified time delay in milliseconds from the current time.voiddispatchApply(int iterations, DispatchIterationKernel iterationKernel) Performs all the specified iterations of the kernel asynchronously and waits for them to complete.voiddispatchAsync(Runnable rawOperation) submit the operation for execution on the queue without waiting for completionvoiddispatchAsync(DispatchGroup group, Runnable rawOperation) submit the operation for execution on the queue and add it to the specified group without waiting for completionprotected voiddispatchAsync(DispatchGroup group, Runnable rawOperation, boolean isBarrier) submit the operation for execution on the queue and add it to the specified group without waiting for completionvoiddispatchBarrierAsync(Runnable operation) Submit a barrier block for execution on the queue without waiting for completion.voiddispatchBarrierSync(Runnable operation) Submit a barrier block for execution on the queue and wait for completion.voiddispatchSync(Runnable rawOperation) submit the operation for execution on the queue and wait for it to completeprotected voiddispatchSync(Runnable rawOperation, boolean isBarrier) submit the operation for execution on the queue and wait for it to complete<T> TdispatchSync(Callable<T> rawOperation) submit the operation for execution on the queue and wait for it to completevoiddispose()dispose of this queue - can only be called on a custom queueprotected <T> voidenqueueOperation(xal.tools.dispatch.DispatchOperation<T> operation) Enqueue the operation and process make sure the operation queue gets processedprotected voidfinalize()dispose of the executorsstatic DispatchQueueget the current queue or null if the current thread does not belong to a queuestatic DispatchQueueGet the global background priority queuestatic DispatchQueueGet the global default priority queuestatic DispatchQueueGet the global high priority queuestatic DispatchQueueGet the global low priority queuestatic DispatchQueuegetGlobalQueue(int priority) Get the global queue corresponding to the specified priority.getLabel()get this queue's labelstatic DispatchQueueGet the main queue on which Swing events are dispatched.protected intincrement the running operations countbooleanDetermine whether this queue is the current queuebooleandetermine whether this queue has been disposedbooleanDetermines 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> voidoperationCompleted(xal.tools.dispatch.DispatchOperation<T> operation) Event indicating that an operation in this group has completedprotected <T> voidpostProcessOperation(xal.tools.dispatch.DispatchOperation<T> operation) call this method when an operation has completed executionprotected abstract voidProcess the operation queue by processing the next pending operation using the serial queue processor to guarantee the operations are queue seriallyprotected voidrelease allocated resources - called internally for any queuevoidresume()resume execution of pending operations if suspended or throw an exception if attempting to resume a disposed queuevoidsuspend()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
-