Package com.spotify.futures
Class ConcurrencyReducer<T>
java.lang.Object
com.spotify.futures.ConcurrencyReducer<T>
ConcurrencyReducer is used to queue tasks which will be
executed in a manner reducing the number of concurrent tasks.
Note: This is a port of ConcurrencyLimiter from futures-extra for use with CompletionStages-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classprivate static class -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Semaphoreprivate final intprivate final intprivate final BlockingQueue<ConcurrencyReducer.Job<T>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionadd(Callable<? extends CompletionStage<T>> callable) the callable function will run as soon as the currently active set of futures is less than the maxConcurrency limit.static <T> ConcurrencyReducer<T>create(int maxConcurrency, int maxQueueSize) private ConcurrencyReducer.Job<T>grabJob()Does one of two things: 1) return a job and acquire a permit from the semaphore 2) return null and does not acquire a permit from the semaphoreprivate voidinvoke(CompletableFuture<T> response, Callable<? extends CompletionStage<T>> callable) intintprivate voidpump()intint
-
Field Details
-
queue
-
limit
-
maxQueueSize
private final int maxQueueSize -
maxConcurrency
private final int maxConcurrency
-
-
Constructor Details
-
ConcurrencyReducer
private ConcurrencyReducer(int maxConcurrency, int maxQueueSize)
-
-
Method Details
-
create
- Parameters:
maxConcurrency- maximum number of futures in progress,maxQueueSize- maximum number of jobs in queue. This is a soft bound and may be temporarily exceeded if add() is called concurrently.- Returns:
- a new concurrency limiter
-
add
the callable function will run as soon as the currently active set of futures is less than the maxConcurrency limit.- Parameters:
callable- - a function that creates a future.- Returns:
- a proxy future that completes with the future created by the input function. This
future will be immediately failed with
ConcurrencyReducer.CapacityReachedExceptionif the soft queue size limit is exceeded.
-
numQueued
public int numQueued()- Returns:
- the number of callables that are queued up and haven't started yet.
-
numActive
public int numActive()- Returns:
- the number of currently active futures that have not yet completed.
-
remainingQueueCapacity
public int remainingQueueCapacity()- Returns:
- the number of additional callables that can be queued before failing.
-
remainingActiveCapacity
public int remainingActiveCapacity()- Returns:
- the number of additional callables that can be run without queueing.
-
grabJob
Does one of two things: 1) return a job and acquire a permit from the semaphore 2) return null and does not acquire a permit from the semaphore -
pump
private void pump() -
invoke
-