Class SemaphoredDelegatingExecutor

java.lang.Object
org.apache.hadoop.thirdparty.com.google.common.collect.ForwardingObject
org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
org.apache.hadoop.util.SemaphoredDelegatingExecutor
All Implemented Interfaces:
Executor, ExecutorService
Direct Known Subclasses:
BlockingThreadPoolExecutorService

@Private public class SemaphoredDelegatingExecutor extends org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
This ExecutorService blocks the submission of new tasks when its queue is already full by using a semaphore. Task submissions require permits, task completions release permits.

This is a refactoring of BlockingThreadPoolExecutorService; that code contains the thread pool logic, whereas this isolates the semaphore and submit logic for use with other thread pools and delegation models.

This is inspired by this s4 threadpool

  • Constructor Details

    • SemaphoredDelegatingExecutor

      public SemaphoredDelegatingExecutor(ExecutorService executorDelegatee, int permitCount, boolean fair, DurationTrackerFactory trackerFactory)
      Instantiate.
      Parameters:
      executorDelegatee - Executor to delegate to
      permitCount - number of permits into the queue permitted
      fair - should the semaphore be "fair"
      trackerFactory - duration tracker factory.
    • SemaphoredDelegatingExecutor

      public SemaphoredDelegatingExecutor(ExecutorService executorDelegatee, int permitCount, boolean fair)
      Instantiate without collecting executor aquisition duration information.
      Parameters:
      executorDelegatee - Executor to delegate to
      permitCount - number of permits into the queue permitted
      fair - should the semaphore be "fair"
  • Method Details

    • delegate

      protected ExecutorService delegate()
      Specified by:
      delegate in class org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
    • invokeAll

      public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks) throws InterruptedException
      Specified by:
      invokeAll in interface ExecutorService
      Overrides:
      invokeAll in class org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
      Throws:
      InterruptedException
    • invokeAll

      public <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException
      Specified by:
      invokeAll in interface ExecutorService
      Overrides:
      invokeAll in class org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
      Throws:
      InterruptedException
    • invokeAny

      public <T> T invokeAny(Collection<? extends Callable<T>> tasks) throws InterruptedException, ExecutionException
      Specified by:
      invokeAny in interface ExecutorService
      Overrides:
      invokeAny in class org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
      Throws:
      InterruptedException
      ExecutionException
    • invokeAny

      public <T> T invokeAny(Collection<? extends Callable<T>> tasks, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException
      Specified by:
      invokeAny in interface ExecutorService
      Overrides:
      invokeAny in class org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
      Throws:
      InterruptedException
      ExecutionException
      TimeoutException
    • submit

      public <T> Future<T> submit(Callable<T> task)
      Specified by:
      submit in interface ExecutorService
      Overrides:
      submit in class org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
    • submit

      public <T> Future<T> submit(Runnable task, T result)
      Specified by:
      submit in interface ExecutorService
      Overrides:
      submit in class org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
    • submit

      public Future<?> submit(Runnable task)
      Specified by:
      submit in interface ExecutorService
      Overrides:
      submit in class org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
    • execute

      public void execute(Runnable command)
      Specified by:
      execute in interface Executor
      Overrides:
      execute in class org.apache.hadoop.thirdparty.com.google.common.util.concurrent.ForwardingExecutorService
    • getAvailablePermits

      public int getAvailablePermits()
      Get the number of permits available; guaranteed to be 0 <= availablePermits <= size.
      Returns:
      the number of permits available at the time of invocation.
    • getWaitingCount

      public int getWaitingCount()
      Get the number of threads waiting to acquire a permit.
      Returns:
      snapshot of the length of the queue of blocked threads.
    • getPermitCount

      public int getPermitCount()
      Total number of permits.
      Returns:
      the number of permits as set in the constructor
    • toString

      public String toString()
      Overrides:
      toString in class org.apache.hadoop.thirdparty.com.google.common.collect.ForwardingObject