Class PollingWatchdog
- java.lang.Object
-
- org.threadly.concurrent.future.watchdog.PollingWatchdog
-
public class PollingWatchdog extends java.lang.Object
This class is designed to help ensure a future completes with an arbitrary condition to determine when a future should be canceled. If the condition is time based then seeConstantTimeWatchdog
andMixedTimeWatchdog
as possibly more efficient options. This class however can allow the condition to be independent from the submission to be watched. For example if you want to time something out after a lack of progress, but don't care about the absolute time it has been running you can track that progress and provide aSupplier
towatch(Supplier, ListenableFuture)
which will returntrue
only once there has been no progress in X milliseconds. The supplier will be invoked regularly to check the state associated to the matching future.- Since:
- 5.40
-
-
Constructor Summary
Constructors Constructor Description PollingWatchdog(long pollFrequencyMillis, boolean sendInterruptOnFutureCancel)
Constructs a newPollingWatchdog
.PollingWatchdog(SubmitterScheduler scheduler, long pollFrequencyMillis, boolean sendInterruptOnFutureCancel)
Constructs a newPollingWatchdog
with a scheduler of your choosing.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description 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(java.util.function.Supplier<java.lang.Boolean> cancelTest, ListenableFuture<?> future)
Watch a givenListenableFuture
to ensure that it completes within the constructed time limit.
-
-
-
Constructor Detail
-
PollingWatchdog
public PollingWatchdog(long pollFrequencyMillis, boolean sendInterruptOnFutureCancel)
Constructs a newPollingWatchdog
. 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:
pollFrequencyMillis
- Time in milliseconds that futures will be checked if they should be timed outsendInterruptOnFutureCancel
- Iftrue
, and a thread is provided with the future, an interrupt will be sent on timeout
-
PollingWatchdog
public PollingWatchdog(SubmitterScheduler scheduler, long pollFrequencyMillis, boolean sendInterruptOnFutureCancel)
Constructs a newPollingWatchdog
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 futurespollFrequencyMillis
- Time in milliseconds that futures will be checked if they should be timed outsendInterruptOnFutureCancel
- Iftrue
, and a thread is provided with the future, an interrupt will be sent on timeout
-
-
Method Detail
-
watch
public void watch(java.util.function.Supplier<java.lang.Boolean> cancelTest, 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 thisPollingWatchdog
was constructed.The provided
Supplier
will be invoked regularly as the state is polled. Once it returnstrue
it will never be invoked again, and insteadFuture.cancel(boolean)
will be invoked on the provided future.- Parameters:
cancelTest
- Supplier to returntrue
when the future should be canceledfuture
- 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
-
-