Package com.github.phantomthief.util
Class DeadlineChecker
- java.lang.Object
-
- com.github.phantomthief.util.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 Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()static DeadlineCheckerdeadlineWithMinTicker(Duration minTicker)<X extends Throwable>
voidrunWithDeadline(ThrowableRunnable<X> runnable, Duration deadline, Consumer<Thread> deadlineExceeded)<T,X extends Throwable>
TsupplyWithDeadline(ThrowableSupplier<T,X> supplier, Duration deadline, Consumer<Thread> deadlineExceeded)
-
-
-
Method Detail
-
deadlineWithMinTicker
public static DeadlineChecker deadlineWithMinTicker(Duration minTicker)
-
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 threaddeadlineExceeded- 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 threaddeadlineExceeded- 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
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
-