public class MixedTimeWatchdog
extends java.lang.Object
ConstantTimeWatchdog
instances. Because the
timeout for ConstantTimeWatchdog
is set in the constructor
ConstantTimeWatchdog.ConstantTimeWatchdog(long, boolean)
, you can
use this class to be more flexible and set the timeout at the time of watching the future.
Using CancelDebuggingListenableFuture
to wrap the futures
before providing to this class can provide an easier understanding of the state of a Future when
it was timed out by this class.
Constructor and Description |
---|
MixedTimeWatchdog(SubmitterScheduler scheduler,
boolean sendInterruptOnFutureCancel)
Constructs a new
MixedTimeWatchdog with a scheduler of your choosing. |
MixedTimeWatchdog(SubmitterScheduler scheduler,
boolean sendInterruptOnFutureCancel,
long resolutionMillis)
Constructs a new
MixedTimeWatchdog with a scheduler of your choosing. |
Modifier and Type | Method and Description |
---|---|
static MixedTimeWatchdog |
centralWatchdog(boolean sendInterruptOnFutureCancel)
Return a static / shared
MixedTimeWatchdog instance. |
int |
getWatchingCount()
Check how many futures are currently being monitored for completion.
|
void |
watch(long timeoutInMillis,
ListenableFuture<?> future)
Watch a given
ListenableFuture to ensure that it completes within the provided
time limit. |
public MixedTimeWatchdog(SubmitterScheduler scheduler, boolean sendInterruptOnFutureCancel)
MixedTimeWatchdog
with a scheduler of your choosing. It is critical
that this scheduler has a free thread available to inspect futures which may not have
completed in the given timeout. You may want to use a org.threadly.concurrent.limiter to
ensure that there are threads available.scheduler
- Scheduler to schedule task to look for expired futuressendInterruptOnFutureCancel
- If true
, and a thread is provided with the future,
an interrupt will be sent on timeoutpublic MixedTimeWatchdog(SubmitterScheduler scheduler, boolean sendInterruptOnFutureCancel, long resolutionMillis)
MixedTimeWatchdog
with a scheduler of your choosing. It is critical
that this scheduler has a free thread available to inspect futures which may not have
completed in the given timeout. You may want to use a org.threadly.concurrent.limiter to
ensure that there are threads available.
This constructor allows you to set the timeout resolutions. Setting the resolution too large can result in futures timing out later than you expected. Setting it too low results in heavy memory consumption when used with a wide variety of timeouts.
scheduler
- Scheduler to schedule task to look for expired futuressendInterruptOnFutureCancel
- If true
, and a thread is provided with the future,
an interrupt will be sent on timeoutresolutionMillis
- The resolution to allow timeout granularitypublic static final MixedTimeWatchdog centralWatchdog(boolean sendInterruptOnFutureCancel)
MixedTimeWatchdog
instance. This instance is backed by the
CentralThreadlyPool
which should be fine in most cases, but if
you have specific needs you can construct your own instance by
MixedTimeWatchdog(SubmitterScheduler, boolean)
, or if you need to specify a
specific timeout resolution using the
MixedTimeWatchdog(SubmitterScheduler, boolean, long)
constructor.
As long as those special cases are not needed, using a shared instance allows for potentially improved efficiency.
sendInterruptOnFutureCancel
- If true
, and a thread is provided with the future,
an interrupt will be sent on timeoutMixedTimeWatchdog
with the specified configurationpublic int getWatchingCount()
public void watch(long timeoutInMillis, ListenableFuture<?> future)
ListenableFuture
to ensure that it completes within the provided
time limit. If the future is not marked as done by the time limit then it will be
completed by invoking Future.cancel(boolean)
. Weather a true
or
false
will be provided to interrupt the running thread is dependent on how this
MixedTimeWatchdog
was constructed.timeoutInMillis
- Time in milliseconds that future should be completed withinfuture
- Future to inspect to ensure completion