Class ConstantTimeWatchdog


  • public class ConstantTimeWatchdog
    extends java.lang.Object
    This class is to guarantee that a given ListenableFuture is completed within a timeout. Once the timeout is reached, if the future has not already completed this will attempt to invoke Future.cancel(boolean). The future should then throw a CancellationException on a Future.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 Detail

      • ConstantTimeWatchdog

        public ConstantTimeWatchdog​(long timeoutInMillis,
                                    boolean sendInterruptOnFutureCancel)
        Constructs a new ConstantTimeWatchdog. 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 the CentralThreadlyPool default is not a good option.
        Parameters:
        timeoutInMillis - Time in milliseconds that futures will be set to error if they are not done
        sendInterruptOnFutureCancel - If true, 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 new ConstantTimeWatchdog 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
        timeoutInMillis - Time in milliseconds that futures will be set to error if they are not done
        sendInterruptOnFutureCancel - If true, 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 given ListenableFuture 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 invoking Future.cancel(boolean). Weather a true or false will be provided to interrupt the running thread is dependent on how this ConstantTimeWatchdog 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