Class LimitedExecutor

  • All Implemented Interfaces:
    java.util.concurrent.Executor

    public class LimitedExecutor
    extends java.lang.Object
    implements java.util.concurrent.Executor
    Executes tasks in the given executor, but never has more than maxConcurrentTasks tasks running at the same time.

    A task can finish running without allowing another task to run in its stead, with executeAsync(Supplier). A new task will only start after the CompletableFuture returned by the task has completed.

    Blocking mode. If the executor is a WithinThreadExecutor, tasks will run in the thread that submitted them. If there are no available permits, the caller thread will block until a permit becomes available.

    Since:
    9.0
    Author:
    Dan Berindei
    • Constructor Summary

      Constructors 
      Constructor Description
      LimitedExecutor​(java.lang.String name, java.util.concurrent.Executor executor, int maxConcurrentTasks)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void execute​(java.lang.Runnable command)  
      void executeAsync​(java.util.function.Supplier<java.util.concurrent.CompletionStage<java.lang.Void>> asyncCommand)
      Similar to execute(Runnable), but the task can continue executing asynchronously, without blocking the OS thread, while still counting against this executor's limit.
      void shutdownNow()
      Stops the executor and cancels any queued tasks.
      • Methods inherited from class java.lang.Object

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

      • LimitedExecutor

        public LimitedExecutor​(java.lang.String name,
                               java.util.concurrent.Executor executor,
                               int maxConcurrentTasks)
    • Method Detail

      • shutdownNow

        public void shutdownNow()
        Stops the executor and cancels any queued tasks. Stop and interrupt any tasks that have already been handed to the underlying executor.
      • execute

        public void execute​(java.lang.Runnable command)
        Specified by:
        execute in interface java.util.concurrent.Executor
      • executeAsync

        public void executeAsync​(java.util.function.Supplier<java.util.concurrent.CompletionStage<java.lang.Void>> asyncCommand)
        Similar to execute(Runnable), but the task can continue executing asynchronously, without blocking the OS thread, while still counting against this executor's limit.
        Parameters:
        asyncCommand - A task that returns a non-null CompletionStage, which may be already completed or may complete at some point in the future.