Class DispatchQueue

java.lang.Object
xal.tools.dispatch.DispatchQueue

public abstract class DispatchQueue extends Object
DispatchQueue which attempts to implement a subset of the open source libdispatch library
  • Field Details

    • DISPATCH_QUEUE_PRIORITY_HIGH

      public static final int DISPATCH_QUEUE_PRIORITY_HIGH
      priority for a high priority queue
    • DISPATCH_QUEUE_PRIORITY_DEFAULT

      public static final int DISPATCH_QUEUE_PRIORITY_DEFAULT
      priority for the default priority queue
    • DISPATCH_QUEUE_PRIORITY_LOW

      public static final int DISPATCH_QUEUE_PRIORITY_LOW
      priority for the low priority queue
    • DISPATCH_QUEUE_PRIORITY_BACKGROUND

      public static final int DISPATCH_QUEUE_PRIORITY_BACKGROUND
      priority for the background priority queue
    • queueProcessor

      protected final ExecutorService queueProcessor
      executor which processes the queue
    • dispatchThreadFactory

      protected final ThreadFactory dispatchThreadFactory
      thread factory for dispatch this queue
    • dispatchExecutor

      protected final ExecutorService dispatchExecutor
      executor for processing dispatched operations
    • runningOperationCounter

      protected final AtomicInteger runningOperationCounter
      number of operations currently running
    • queueState

      protected volatile DispatchQueue.DispatchQueueState queueState
      state of this queue
    • pendingOperationQueue

      protected final LinkedBlockingQueue<xal.tools.dispatch.DispatchOperation<?>> pendingOperationQueue
      queue of pending operations which have not yet been submitted for execution
  • Constructor Details

    • DispatchQueue

      protected DispatchQueue(String label, int priority)
      Primary Constructor
    • DispatchQueue

      protected DispatchQueue(String label)
      Constructor
  • Method Details

    • finalize

      protected void finalize() throws Throwable
      dispose of the executors
      Overrides:
      finalize in class Object
      Throws:
      Throwable
    • getLabel

      public String 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

      protected abstract ExecutorService createDispatchExecutor()
      create the executor for dispatching operations
    • createConcurrentQueue

      public static DispatchQueue createConcurrentQueue(String label)
      Create a concurrent queue
      Parameters:
      label - optional label for the queue to create
      Returns:
      a new concurrent queue
    • createSerialQueue

      public static DispatchQueue createSerialQueue(String label)
      Create a serial queue
      Parameters:
      label - optional label for the queue to create
      Returns:
      a new serial queue
    • getMainQueue

      public static DispatchQueue getMainQueue()
      Get the main queue on which Swing events are dispatched. The main queue cannot be suspended or resumed.
    • getGlobalQueue

      public static DispatchQueue getGlobalQueue(int priority)
      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

      public static DispatchQueue getGlobalDefaultPriorityQueue()
      Get the global default priority queue
    • getGlobalHighPriorityQueue

      public static DispatchQueue getGlobalHighPriorityQueue()
      Get the global high priority queue
    • getGlobalLowPriorityQueue

      public static DispatchQueue getGlobalLowPriorityQueue()
      Get the global low priority queue
    • getGlobalBackgroundPriorityQueue

      public static DispatchQueue getGlobalBackgroundPriorityQueue()
      Get the global background priority queue
    • getCurrentQueue

      public static DispatchQueue 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

      public <T> T dispatchSync(Callable<T> rawOperation)
      submit the operation for execution on the queue and wait for it to complete
    • dispatchSync

      public void dispatchSync(Runnable rawOperation)
      submit the operation for execution on the queue and wait for it to complete
    • dispatchSync

      protected void dispatchSync(Runnable rawOperation, boolean isBarrier)
      submit the operation for execution on the queue and wait for it to complete
    • dispatchAsync

      public void dispatchAsync(Runnable rawOperation)
      submit the operation for execution on the queue without waiting for completion
    • dispatchAsync

      public void dispatchAsync(DispatchGroup group, Runnable rawOperation)
      submit the operation for execution on the queue and add it to the specified group without waiting for completion
    • dispatchAsync

      protected 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 completion
    • dispatchAfterDelay

      public void dispatchAfterDelay(long delay, Runnable rawOperation)
      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 run
      rawOperation - the operation to run
    • dispatchAfter

      public void dispatchAfter(Date dispatchTime, Runnable rawOperation)
      dispatch the operation after the specified time without blocking
    • dispatchBarrierAsync

      public void dispatchBarrierAsync(Runnable operation)
      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

      public void dispatchBarrierSync(Runnable operation)
      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

      public void dispatchApply(int iterations, DispatchIterationKernel iterationKernel)
      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

      protected xal.tools.dispatch.DispatchOperation<Void> makeDispatchOperation(Runnable rawOperation)
      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