public class KeyedExecutorLimiter
extends java.lang.Object
KeyDistributedExecutor
and
an ExecutorLimiter
. This is designed to limit concurrency for a given thread, but
permit more than one thread to run at a time for a given key. If the desired effect is to have
a single thread per key, KeyDistributedExecutor
is a
much better option.
The easiest way to use this class would be to have it distribute out executors through
getSubmitterExecutorForKey(Object)
.
Constructor and Description |
---|
KeyedExecutorLimiter(java.util.concurrent.Executor executor,
int maxConcurrency)
Construct a new
KeyedExecutorLimiter providing only the backing executor and the
maximum concurrency per unique key. |
KeyedExecutorLimiter(java.util.concurrent.Executor executor,
int maxConcurrency,
boolean limitFutureListenersExecution)
Construct a new
KeyedExecutorLimiter providing only the backing executor and the
maximum concurrency per unique key. |
KeyedExecutorLimiter(java.util.concurrent.Executor executor,
int maxConcurrency,
java.lang.String subPoolName,
boolean addKeyToThreadName)
Construct a new
KeyedExecutorLimiter providing the backing executor, the maximum
concurrency per unique key, and how keyed limiter threads should be named. |
KeyedExecutorLimiter(java.util.concurrent.Executor executor,
int maxConcurrency,
java.lang.String subPoolName,
boolean addKeyToThreadName,
boolean limitFutureListenersExecution)
Construct a new
KeyedExecutorLimiter providing the backing executor, the maximum
concurrency per unique key, and how keyed limiter threads should be named. |
Modifier and Type | Method and Description |
---|---|
void |
execute(java.lang.Object taskKey,
java.lang.Runnable task)
Provide a task to be run with a given thread key.
|
int |
getMaxConcurrencyPerKey()
Check how many threads may run in parallel for a single unique key.
|
SubmitterExecutor |
getSubmitterExecutorForKey(java.lang.Object taskKey)
Returns an executor implementation where all tasks submitted on this executor will run on the
provided key.
|
int |
getTrackedKeyCount()
Check how many keys are currently being restricted or monitored.
|
int |
getUnsubmittedTaskCount(java.lang.Object taskKey)
Check how many tasks are currently being limited, and not submitted yet for a given key.
|
java.util.Map<java.lang.Object,java.lang.Integer> |
getUnsubmittedTaskCountMap()
Get a map of all the keys and how many tasks are held back (queued) in each limiter per key.
|
void |
setMaxConcurrencyPerKey(int maxConcurrency)
Updates the concurrency limit for each key.
|
<TT> ListenableFuture<TT> |
submit(java.lang.Object taskKey,
java.util.concurrent.Callable<TT> task)
Submit a callable to be run with a given thread key.
|
ListenableFuture<?> |
submit(java.lang.Object taskKey,
java.lang.Runnable task)
Submit a task to be run with a given thread key.
|
<TT> ListenableFuture<TT> |
submit(java.lang.Object taskKey,
java.lang.Runnable task,
TT result)
Submit a task to be run with a given thread key.
|
public KeyedExecutorLimiter(java.util.concurrent.Executor executor, int maxConcurrency)
KeyedExecutorLimiter
providing only the backing executor and the
maximum concurrency per unique key. By default this will not rename threads for tasks
executing.executor
- Executor to execute tasks onmaxConcurrency
- Maximum concurrency allowed per task keypublic KeyedExecutorLimiter(java.util.concurrent.Executor executor, int maxConcurrency, boolean limitFutureListenersExecution)
KeyedExecutorLimiter
providing only the backing executor and the
maximum concurrency per unique key. By default this will not rename threads for tasks
executing.
This constructor allows you to specify if listeners /
FutureCallback
's / functions in
ListenableFuture.map(java.util.function.Function)
or
ListenableFuture.flatMap(java.util.function.Function)
should be counted towards the
concurrency limit. Specifying false
will release the limit as soon as the original
task completes. Specifying true
will continue to enforce the limit until all listeners
(without an executor) complete.
executor
- Executor to execute tasks onmaxConcurrency
- Maximum concurrency allowed per task keylimitFutureListenersExecution
- true
to include listener / mapped functions towards execution limitpublic KeyedExecutorLimiter(java.util.concurrent.Executor executor, int maxConcurrency, java.lang.String subPoolName, boolean addKeyToThreadName)
KeyedExecutorLimiter
providing the backing executor, the maximum
concurrency per unique key, and how keyed limiter threads should be named.executor
- Executor to execute tasks on tomaxConcurrency
- Maximum concurrency allowed per task keysubPoolName
- Name prefix for sub pools, null
to not change thread namesaddKeyToThreadName
- If true
the key's .toString() will be added in the thread namepublic KeyedExecutorLimiter(java.util.concurrent.Executor executor, int maxConcurrency, java.lang.String subPoolName, boolean addKeyToThreadName, boolean limitFutureListenersExecution)
KeyedExecutorLimiter
providing the backing executor, the maximum
concurrency per unique key, and how keyed limiter threads should be named.
This constructor allows you to specify if listeners /
FutureCallback
's / functions in
ListenableFuture.map(java.util.function.Function)
or
ListenableFuture.flatMap(java.util.function.Function)
should be counted towards the
concurrency limit. Specifying false
will release the limit as soon as the original
task completes. Specifying true
will continue to enforce the limit until all listeners
(without an executor) complete.
executor
- Executor to execute tasks on tomaxConcurrency
- Maximum concurrency allowed per task keysubPoolName
- Name prefix for sub pools, null
to not change thread namesaddKeyToThreadName
- If true
the key's .toString() will be added in the thread namelimitFutureListenersExecution
- true
to include listener / mapped functions towards execution limitpublic int getMaxConcurrencyPerKey()
public void setMaxConcurrencyPerKey(int maxConcurrency)
maxConcurrency
- maximum quantity of tasks to run in parallel per keypublic int getTrackedKeyCount()
public int getUnsubmittedTaskCount(java.lang.Object taskKey)
taskKey
- Key which would be limitedpublic java.util.Map<java.lang.Object,java.lang.Integer> getUnsubmittedTaskCountMap()
Because this requires an iteration of all limiters, if only a single limiters unsubmitted
count is needed, use getUnsubmittedTaskCount(Object)
as a cheaper alternative.
public void execute(java.lang.Object taskKey, java.lang.Runnable task)
See also: Executor.execute(Runnable)
taskKey
- object key where equals()
will be used to determine execution threadtask
- Task to be executedpublic ListenableFuture<?> submit(java.lang.Object taskKey, java.lang.Runnable task)
See also: SubmitterExecutor.submit(Runnable)
taskKey
- object key where equals()
will be used to determine execution threadtask
- Task to be executedpublic <TT> ListenableFuture<TT> submit(java.lang.Object taskKey, java.lang.Runnable task, TT result)
TT
- type of result returned from the futuretaskKey
- object key where equals()
will be used to determine execution threadtask
- Runnable to be executedresult
- Result to be returned from future when task completespublic <TT> ListenableFuture<TT> submit(java.lang.Object taskKey, java.util.concurrent.Callable<TT> task)
See also: SubmitterExecutor.submit(Callable)
TT
- type of result returned from the futuretaskKey
- object key where equals()
will be used to determine execution threadtask
- Callable to be executedpublic SubmitterExecutor getSubmitterExecutorForKey(java.lang.Object taskKey)
taskKey
- object key where equals()
will be used to determine execution thread