Class SemaphoreCompletionService<T>

  • All Implemented Interfaces:
    java.util.concurrent.CompletionService<T>

    public class SemaphoreCompletionService<T>
    extends java.lang.Object
    implements java.util.concurrent.CompletionService<T>
    Executes tasks in the given executor, but never has more than maxConcurrentTasks tasks running at the same time.
    Since:
    7.2
    Author:
    Dan Berindei
    • Constructor Summary

      Constructors 
      Constructor Description
      SemaphoreCompletionService​(java.util.concurrent.Executor executor, int maxConcurrentTasks)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.util.concurrent.Future<T> backgroundTaskFinished​(java.util.concurrent.Callable<T> cleanupTask)
      Signal that a task that called continueTaskInBackground() has finished and optionally execute another task on the just-freed thread.
      void cancelQueuedTasks()
      When stopping, cancel any queued tasks.
      void continueTaskInBackground()
      Called from a task to remove the permit that would otherwise be freed when the task finishes running When the asynchronous part of the task finishes, it must call backgroundTaskFinished(Callable) to make the permit available again.
      java.util.List<? extends java.util.concurrent.Future<T>> drainCompletionQueue()  
      java.util.concurrent.Future<T> poll()  
      java.util.concurrent.Future<T> poll​(long timeout, java.util.concurrent.TimeUnit unit)  
      java.util.concurrent.Future<T> submit​(java.lang.Runnable task, T result)  
      java.util.concurrent.Future<T> submit​(java.util.concurrent.Callable<T> task)  
      java.util.concurrent.Future<T> take()  
      • Methods inherited from class java.lang.Object

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

      • SemaphoreCompletionService

        public SemaphoreCompletionService​(java.util.concurrent.Executor executor,
                                          int maxConcurrentTasks)
    • Method Detail

      • drainCompletionQueue

        public java.util.List<? extends java.util.concurrent.Future<T>> drainCompletionQueue()
      • cancelQueuedTasks

        public void cancelQueuedTasks()
        When stopping, cancel any queued tasks.
      • continueTaskInBackground

        public void continueTaskInBackground()
        Called from a task to remove the permit that would otherwise be freed when the task finishes running When the asynchronous part of the task finishes, it must call backgroundTaskFinished(Callable) to make the permit available again.
      • backgroundTaskFinished

        public java.util.concurrent.Future<T> backgroundTaskFinished​(java.util.concurrent.Callable<T> cleanupTask)
        Signal that a task that called continueTaskInBackground() has finished and optionally execute another task on the just-freed thread.
      • submit

        public java.util.concurrent.Future<T> submit​(java.util.concurrent.Callable<T> task)
        Specified by:
        submit in interface java.util.concurrent.CompletionService<T>
      • submit

        public java.util.concurrent.Future<T> submit​(java.lang.Runnable task,
                                                     T result)
        Specified by:
        submit in interface java.util.concurrent.CompletionService<T>
      • take

        public java.util.concurrent.Future<T> take()
                                            throws java.lang.InterruptedException
        Specified by:
        take in interface java.util.concurrent.CompletionService<T>
        Throws:
        java.lang.InterruptedException
      • poll

        public java.util.concurrent.Future<T> poll()
        Specified by:
        poll in interface java.util.concurrent.CompletionService<T>
      • poll

        public java.util.concurrent.Future<T> poll​(long timeout,
                                                   java.util.concurrent.TimeUnit unit)
                                            throws java.lang.InterruptedException
        Specified by:
        poll in interface java.util.concurrent.CompletionService<T>
        Throws:
        java.lang.InterruptedException