Class DeadlineChecker

  • All Implemented Interfaces:
    AutoCloseable

    public class DeadlineChecker
    extends Object
    implements AutoCloseable
    When a task is running. NORMALLY checking if it's slow: 1. create a new thread to check if it run out of deadline by calling Future.get(timeout) 2. when the task was finished, check if it's slow with a stopwatch. while there are some problems: if a task didn't end(or spend too much time), it could never found slow without spawn a thread, or found the deadline exceeded on time. This helper make another solution: Gather running tasks in a container and check if there is a deadline occurred periodically.
    Author:
    w.vela Created on 2017-02-24.
    • Method Detail

      • runWithDeadline

        public <X extends Throwable> void runWithDeadline​(ThrowableRunnable<X> runnable,
                                                          Duration deadline,
                                                          Consumer<Thread> deadlineExceeded)
                                                   throws X extends Throwable
        Parameters:
        runnable - main task which would be run on caller's thread
        deadlineExceeded - triggered in another thread when the main task's deadline exceeded. the consumer would be running in ticker's thread, so it should be short and simple.
        Throws:
        X extends Throwable
      • supplyWithDeadline

        public <T,​X extends Throwable> T supplyWithDeadline​(ThrowableSupplier<T,​X> supplier,
                                                                  Duration deadline,
                                                                  Consumer<Thread> deadlineExceeded)
                                                           throws X extends Throwable
        Parameters:
        supplier - main task which would be run on caller's thread
        deadlineExceeded - triggered in another thread when the main task's deadline exceeded. the consumer would be running in ticker's thread, so it should be short and simple.
        Throws:
        X extends Throwable