Class ConstantTimeWatchdog
- java.lang.Object
-
- org.threadly.concurrent.future.watchdog.ConstantTimeWatchdog
-
public class ConstantTimeWatchdog extends java.lang.Object
This class is to guarantee that a givenListenableFuture
is completed within a timeout. Once the timeout is reached, if the future has not already completed this will attempt to invokeFuture.cancel(boolean)
. The future should then throw aCancellationException
on aFuture.get()
call.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.Watchdog)
-
-
Constructor Summary
Constructors Constructor Description ConstantTimeWatchdog(long timeoutInMillis, boolean sendInterruptOnFutureCancel)
Constructs a newConstantTimeWatchdog
.ConstantTimeWatchdog(SubmitterScheduler scheduler, long timeoutInMillis, boolean sendInterruptOnFutureCancel)
Constructs a newConstantTimeWatchdog
with a scheduler of your choosing.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description long
getTimeoutInMillis()
Request the timeout in milliseconds until futures that have not completed are canceled.int
getWatchingCount()
Check how many futures are currently being monitored for completion.boolean
isActive()
Checks to see if this watchdog is currently active.void
watch(ListenableFuture<?> future)
Watch a givenListenableFuture
to ensure that it completes within the constructed time limit.
-
-
-
Constructor Detail
-
ConstantTimeWatchdog
public ConstantTimeWatchdog(long timeoutInMillis, boolean sendInterruptOnFutureCancel)
Constructs a newConstantTimeWatchdog
. This constructor will use a default static scheduler (which is lazily constructed). This should be fine in most cases, but you can provide your own scheduler if you have specific needs where theCentralThreadlyPool
default is not a good option.- Parameters:
timeoutInMillis
- Time in milliseconds that futures will be set to error if they are not donesendInterruptOnFutureCancel
- Iftrue
, and a thread is provided with the future, an interrupt will be sent on timeout
-
ConstantTimeWatchdog
public ConstantTimeWatchdog(SubmitterScheduler scheduler, long timeoutInMillis, boolean sendInterruptOnFutureCancel)
Constructs a newConstantTimeWatchdog
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 futurestimeoutInMillis
- Time in milliseconds that futures will be set to error if they are not donesendInterruptOnFutureCancel
- Iftrue
, and a thread is provided with the future, an interrupt will be sent on timeout
-
-
Method Detail
-
getTimeoutInMillis
public long getTimeoutInMillis()
Request the timeout in milliseconds until futures that have not completed are canceled. This is the timeout that the class was constructed with (since it can not be changed after construction).- Returns:
- Time in milliseconds till incomplete futures have
Future.cancel(boolean)
invoked
-
watch
public void watch(ListenableFuture<?> future)
Watch a givenListenableFuture
to ensure that it completes within the constructed 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 thisConstantTimeWatchdog
was constructed.- Parameters:
future
- Future to inspect to ensure completion
-
isActive
public boolean isActive()
Checks to see if this watchdog is currently active. Meaning there are futures on it which either have not been completed yet, or have not been inspected for completion. If this returns false, it means that there are no futures waiting to complete, and no scheduled tasks currently scheduled to inspect them.- Returns:
true
if this watchdog is currently in use
-
getWatchingCount
public int getWatchingCount()
Check how many futures are currently being monitored for completion.- Returns:
- The number of futures being monitored
-
-