T - The type of elements for which a delay is required.public class Delayer<T>
extends java.lang.Object
| Modifier and Type | Class and Description |
|---|---|
protected class |
Delayer.Resetter
A runnable for resetting the try count and thus remove all delays.
|
| Modifier and Type | Field and Description |
|---|---|
protected LoggingScheduledThreadPoolExecutor |
executor
The executor service for cleaning up after a specified time.
|
protected static long |
MAX_DELAY_PER_TRY
The maximum delay per try in milliseconds.
|
protected int |
maxDelayTry
The amount of tries that lead to a delay just below
maximum delay per try.
|
protected long |
pendingTries
The time in milliseconds the try count keeps pending and is not reset.
|
protected java.util.Map<T,java.util.concurrent.ScheduledFuture<?>> |
resetFutures
The futures for the resetting runnables.
|
protected java.util.Map<T,Pair<java.util.concurrent.atomic.AtomicInteger,java.lang.Runnable>> |
tryCounts
The number of tries of an element (and the corresponding resetting
runnable).
|
protected int |
undelayedTries
The number of tries, that are not subject of a delay.
|
| Constructor and Description |
|---|
Delayer(java.lang.String name,
int undelayedTries)
Creates a new delayer with the designated name and the designated amount of
tries without a delay, delaying a call dependent of the element and the
amount of tries for the element.
|
| Modifier and Type | Method and Description |
|---|---|
void |
delay(T element)
Delays the current thread depending on the current amount of tries for the
designated element needs a delay.
|
protected long |
getDelay(int tryCount)
Calculates the delay (in milliseconds) for the designated try count.
|
void |
reset(T element)
Resets the try count and delay for the designated element.
|
protected static final long MAX_DELAY_PER_TRY
protected final LoggingScheduledThreadPoolExecutor executor
protected final int undelayedTries
protected final long pendingTries
protected int maxDelayTry
protected final java.util.Map<T,Pair<java.util.concurrent.atomic.AtomicInteger,java.lang.Runnable>> tryCounts
AtomicInteger is used here since it is mutable in contrast to
Integer and thus avoids the creation of numerous (immutable)
Pairs.
protected final java.util.Map<T,java.util.concurrent.ScheduledFuture<?>> resetFutures
public Delayer(java.lang.String name,
int undelayedTries)
name - The name of the delayer (used for internal scheduler thread).undelayedTries - The number of tries that are not subject of delay. As
soon as this number is exceeded, a delay will take place.public void delay(T element) throws java.lang.InterruptedException
element - The element for which to count a try and possibly delay.java.lang.InterruptedException - If the waiting thread is interrupted while
waiting for the delay, an InterruptedException will
be thrown. Since this allows to circumvent the delay, callers
must not ignore the interrupt but handle it appropriately for
instance by shutting down.public void reset(T element)
element - The element for which to reset the try count and the delay.protected long getDelay(int tryCount)
tryCount - The amount of tries that have been used yet.