Package io.airlift.concurrent
Class AsyncSemaphore<T,R>
java.lang.Object
io.airlift.concurrent.AsyncSemaphore<T,R>
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 Summary
ConstructorsConstructorDescriptionAsyncSemaphore(int maxPermits, Executor submitExecutor, Function<T, com.google.common.util.concurrent.ListenableFuture<R>> submitter) -
Method Summary
Modifier and TypeMethodDescriptionstatic <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 toFutures.allAsList(ListenableFuture[])) with limiting the number of tasks running in parallel.com.google.common.util.concurrent.ListenableFuture<R>
-
Constructor Details
-
AsyncSemaphore
-
-
Method Details
-
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 toFutures.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 processsubmitter- task submittermaxConcurrency- maximum number of tasks allowed to run in parallelsubmitExecutor- task submission executor- Returns:
ListenableFuturecontaining a list of values returned by thetasks. The order of elements in the list matches the order oftasks. 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
-