Class MixedTimeWatchdog
- java.lang.Object
-
- org.threadly.concurrent.future.watchdog.MixedTimeWatchdog
-
public class MixedTimeWatchdog extends java.lang.Object
A class which handles a collection ofConstantTimeWatchdog
instances. Because the timeout forConstantTimeWatchdog
is set in the constructorConstantTimeWatchdog(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.- Since:
- 5.40 (existed since 4.0.0 as org.threadly.concurrent.future.WatchdogCache)
-
-
Constructor Summary
Constructors Constructor Description MixedTimeWatchdog(SubmitterScheduler scheduler, boolean sendInterruptOnFutureCancel)
Constructs a newMixedTimeWatchdog
with a scheduler of your choosing.MixedTimeWatchdog(SubmitterScheduler scheduler, boolean sendInterruptOnFutureCancel, long resolutionMillis)
Constructs a newMixedTimeWatchdog
with a scheduler of your choosing.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static MixedTimeWatchdog
centralWatchdog(boolean sendInterruptOnFutureCancel)
Return a static / sharedMixedTimeWatchdog
instance.int
getWatchingCount()
Check how many futures are currently being monitored for completion.void
watch(long timeoutInMillis, ListenableFuture<?> future)
Watch a givenListenableFuture
to ensure that it completes within the provided time limit.
-
-
-
Constructor Detail
-
MixedTimeWatchdog
public MixedTimeWatchdog(SubmitterScheduler scheduler, boolean sendInterruptOnFutureCancel)
Constructs a newMixedTimeWatchdog
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.- Parameters:
scheduler
- Scheduler to schedule task to look for expired futuressendInterruptOnFutureCancel
- Iftrue
, and a thread is provided with the future, an interrupt will be sent on timeout
-
MixedTimeWatchdog
public MixedTimeWatchdog(SubmitterScheduler scheduler, boolean sendInterruptOnFutureCancel, long resolutionMillis)
Constructs a newMixedTimeWatchdog
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.
- Parameters:
scheduler
- Scheduler to schedule task to look for expired futuressendInterruptOnFutureCancel
- Iftrue
, and a thread is provided with the future, an interrupt will be sent on timeoutresolutionMillis
- The resolution to allow timeout granularity
-
-
Method Detail
-
centralWatchdog
public static final MixedTimeWatchdog centralWatchdog(boolean sendInterruptOnFutureCancel)
Return a static / sharedMixedTimeWatchdog
instance. This instance is backed by theCentralThreadlyPool
which should be fine in most cases, but if you have specific needs you can construct your own instance byMixedTimeWatchdog(SubmitterScheduler, boolean)
, or if you need to specify a specific timeout resolution using theMixedTimeWatchdog(SubmitterScheduler, boolean, long)
constructor.As long as those special cases are not needed, using a shared instance allows for potentially improved efficiency.
- Parameters:
sendInterruptOnFutureCancel
- Iftrue
, and a thread is provided with the future, an interrupt will be sent on timeout- Returns:
- A shared
MixedTimeWatchdog
with the specified configuration
-
getWatchingCount
public int getWatchingCount()
Check how many futures are currently being monitored for completion.- Returns:
- The number of futures being monitored
-
watch
public void watch(long timeoutInMillis, ListenableFuture<?> future)
Watch a givenListenableFuture
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 invokingFuture.cancel(boolean)
. Weather atrue
orfalse
will be provided to interrupt the running thread is dependent on how thisMixedTimeWatchdog
was constructed.- Parameters:
timeoutInMillis
- Time in milliseconds that future should be completed withinfuture
- Future to inspect to ensure completion
-
-