Class ExecutionQueue


  • public class ExecutionQueue
    extends java.lang.Object
    Queues up submitted Runnables and executes them in serial on an ExecutorService.
    • Constructor Summary

      Constructors 
      Constructor Description
      ExecutionQueue​(java.util.concurrent.Executor service)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean isInQueue()  
      void pause()
      Pause execution of queued Runnables.
      void requireInQueue()
      Call to check if the current code is being executed inside this queue.
      void requireInQueue​(java.lang.String message)
      Call to check if the current code is being executed inside this queue.
      void resume()
      Resume execution of queued Runnables.
      <E extends java.lang.Throwable>
      java.util.concurrent.CompletableFuture<java.lang.Void>
      runOrSubmit​(FragileRunnable<E> fragileRunnable)
      Runs the given FragileRunnable on the calling thread if isInQueue() is true.
      <T,​E extends java.lang.Throwable>
      java.util.concurrent.CompletableFuture<T>
      runOrSubmit​(FragileSupplier<T,​E> fragileSupplier)
      Runs the given FragileSupplier on the calling thread if isInQueue() is true.
      void submit​(java.lang.Runnable runnable)
      Submit a Runnable to be executed.
      void submit​(java.lang.Runnable... runnables)
      Submit one or more Runnables to be executed.
      void submitOrRun​(java.lang.Runnable runnable)
      Run runnable if isInQueue(), otherwise submit(Runnable) it for later execution on the queue.
      void submitToHead​(java.lang.Runnable runnable)
      Submit a Runnable to be executed at the head of the queue.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • ExecutionQueue

        public ExecutionQueue​(java.util.concurrent.Executor service)
    • Method Detail

      • submit

        public void submit​(java.lang.Runnable runnable)
        Submit a Runnable to be executed.
        Parameters:
        runnable - the Runnable to be executed.
      • submit

        public void submit​(java.lang.Runnable... runnables)
        Submit one or more Runnables to be executed.
        Parameters:
        runnables - the Runnables to be executed.
      • submitToHead

        public void submitToHead​(java.lang.Runnable runnable)
        Submit a Runnable to be executed at the head of the queue.
        Parameters:
        runnable - the Runnable to be executed.
      • submitOrRun

        public void submitOrRun​(@Nonnull
                                java.lang.Runnable runnable)
        Run runnable if isInQueue(), otherwise submit(Runnable) it for later execution on the queue.
        Parameters:
        runnable - the Runnable to run on-queue.
      • runOrSubmit

        @Nonnull
        public <E extends java.lang.Throwable> java.util.concurrent.CompletableFuture<java.lang.Void> runOrSubmit​(FragileRunnable<E> fragileRunnable)
        Runs the given FragileRunnable on the calling thread if isInQueue() is true. Queues the given FragileRunnable to be run on-queue if isInQueue() is false. Useful for cases where the caller wishes to block until the given FragileRunnable completes execution but the caller may or may not already be running on-queue. If the FragileRunnable throws a Throwable during execution, it will be caught and used to complete the returned CompletableFuture exceptionally.
        Parameters:
        fragileRunnable - the FragileRunnable to run on-queue. must not be null.
        Returns:
        a CompletableFuture which will be completed after the given FragileRunnable is ran. it will already be completed when called on a thread where isInQueue() is true, else it will be completed once the queue runs the given FragileRunnable.
        Throws:
        java.lang.NullPointerException - if the given FragileRunnable is null
      • runOrSubmit

        @Nonnull
        public <T,​E extends java.lang.Throwable> java.util.concurrent.CompletableFuture<T> runOrSubmit​(FragileSupplier<T,​E> fragileSupplier)
        Runs the given FragileSupplier on the calling thread if isInQueue() is true. Queues the given FragileSupplier to be run on-queue if isInQueue() is false. Useful for cases where the caller wishes to block until the given FragileSupplier completes execution but the caller may or may not already be running on-queue. If the FragileSupplier throws a Throwable during execution, it will be caught and used to complete the returned CompletableFuture exceptionally.
        Parameters:
        fragileSupplier - the FragileSupplier to run on-queue. must not be null.
        Returns:
        a CompletableFuture which will be completed after the given FragileSupplier is ran with the value returned by calling FragileSupplier.get(). it will already be completed when called on a thread where isInQueue() is true, else it will be completed once the queue runs the given FragileSupplier.
        Throws:
        java.lang.NullPointerException - if the given FragileSupplier is null
      • pause

        public void pause()
        Pause execution of queued Runnables.
      • resume

        public void resume()
        Resume execution of queued Runnables.
      • isInQueue

        public boolean isInQueue()
        Returns:
        true if this thread is executing inside this queue.
      • requireInQueue

        public void requireInQueue()
        Call to check if the current code is being executed inside this queue.
        Throws:
        java.lang.IllegalStateException - if flow reaches here without going through this queue
      • requireInQueue

        public void requireInQueue​(java.lang.String message)
        Call to check if the current code is being executed inside this queue.
        Parameters:
        message - The message to use in the thrown exception if the check fails
        Throws:
        java.lang.IllegalStateException - if flow reaches here without going through this queue