Class MixedTimeWatchdog


  • public class MixedTimeWatchdog
    extends java.lang.Object
    A class which handles a collection of ConstantTimeWatchdog instances. Because the timeout for ConstantTimeWatchdog is set in the constructor 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.

    Since:
    5.40 (existed since 4.0.0 as org.threadly.concurrent.future.WatchdogCache)
    • Constructor Detail

      • MixedTimeWatchdog

        public MixedTimeWatchdog​(SubmitterScheduler scheduler,
                                 boolean sendInterruptOnFutureCancel)
        Constructs a new 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.
        Parameters:
        scheduler - Scheduler to schedule task to look for expired futures
        sendInterruptOnFutureCancel - If true, 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 new 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.

        Parameters:
        scheduler - Scheduler to schedule task to look for expired futures
        sendInterruptOnFutureCancel - If true, and a thread is provided with the future, an interrupt will be sent on timeout
        resolutionMillis - The resolution to allow timeout granularity
    • Method Detail

      • 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 given 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.
        Parameters:
        timeoutInMillis - Time in milliseconds that future should be completed within
        future - Future to inspect to ensure completion