final class ThreadMonitor extends Object implements Runnable
This works by sleeping until the specified timeout amount and then interrupting the thread being monitored. If the
thread being monitored completes its work before being interrupted, it should interrupt() the monitor
thread.
Duration timeout = Duration.ofSeconds(1);
try {
Thread monitor = ThreadMonitor.start(timeout);
// do some work here
ThreadMonitor.stop(monitor);
} catch (InterruptedException e) {
// timed amount was reached
}
| Modifier and Type | Method and Description |
|---|---|
void |
run()
Sleeps until the specified timeout amount and then interrupt the thread being monitored.
|
(package private) static Thread |
start(java.time.Duration timeout)
Starts monitoring the current thread.
|
(package private) static Thread |
start(Thread thread,
java.time.Duration timeout)
Starts monitoring the specified thread.
|
(package private) static void |
stop(Thread thread)
Stops monitoring the specified thread.
|
public void run()
run in interface RunnableRunnable.run()static Thread start(java.time.Duration timeout)
timeout - The timeout amount. or no timeout if the value is zero or less.null if the timeout amount is not greater than zero.static Thread start(Thread thread, java.time.Duration timeout)
thread - The thread to monitortimeout - The timeout amount. or no timeout if the value is zero or less.null if the timeout amount is not greater than zero.static void stop(Thread thread)
thread - The monitor thread, may be null.