Class AsyncSemaphore<T,​R>


  • @ThreadSafe
    public class AsyncSemaphore<T,​R>
    extends Object
    Guarantees that no more than maxPermits of tasks will be run concurrently. The class will rely on the ListenableFuture returned by the submitter function to determine when a task has been completed. The submitter function NEEDS to be thread-safe and is recommended to do the bulk of its work asynchronously.
    • Constructor Detail

      • AsyncSemaphore

        public AsyncSemaphore​(int maxPermits,
                              Executor submitExecutor,
                              Function<T,​com.google.common.util.concurrent.ListenableFuture<R>> submitter)
    • Method Detail

      • processAll

        public static <T,​R> com.google.common.util.concurrent.ListenableFuture<List<R>> processAll​(List<T> tasks,
                                                                                                         Function<T,​com.google.common.util.concurrent.ListenableFuture<R>> submitter,
                                                                                                         int maxConcurrency,
                                                                                                         Executor submitExecutor)
        Process a list of tasks as a single unit (similar to Futures.allAsList(ListenableFuture[])) with limiting the number of tasks running in parallel.

        This method may be useful for limiting the number of concurrent requests sent to a remote server when trying to load multiple related entities concurrently:

        For example:

        
         List<Integer> userIds = Lists.of(1, 2, 3);
         ListenableFuture<List<UserInfo>> future = processAll(ids, client::getUserInfoById, 2, executor);
         List<UserInfo> userInfos = future.get(...);
         
        Parameters:
        tasks - tasks to process
        submitter - task submitter
        maxConcurrency - maximum number of tasks allowed to run in parallel
        submitExecutor - task submission executor
        Returns:
        ListenableFuture containing a list of values returned by the tasks. The order of elements in the list matches the order of tasks. If the result future is cancelled all the remaining tasks are cancelled (submitted tasks will be cancelled, pending tasks will not be submitted). If any of the submitted tasks fails or are cancelled, the result future is too. If any of the submitted tasks fails or are cancelled, the remaining tasks are cancelled.
      • submit

        public com.google.common.util.concurrent.ListenableFuture<R> submit​(T task)