/** * Create a thread that will call the supplied function when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * <p> * The thread checks the elapsed time every 100 milliseconds. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @param uponTimeout the function to be called when the maximum amount of time has elapsed; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread timeout(String threadName, long timeout, TimeUnit timeoutUnit, TimeSince elapsedTimer, Runnable uponTimeout) { return timeout(threadName, timeout, timeoutUnit, 100, TimeUnit.MILLISECONDS, elapsedTimer::elapsedTime, elapsedTimer::reset, uponTimeout); }
/** * Create a thread that will call the supplied function when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * <p> * The thread checks the elapsed time every 100 milliseconds. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param sleepInterval the amount of time for the new thread to sleep after checking the elapsed time; must be positive * @param sleepUnit the unit for {@code sleepInterval}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @param uponTimeout the function to be called when the maximum amount of time has elapsed; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread timeout(String threadName, long timeout, TimeUnit timeoutUnit, long sleepInterval, TimeUnit sleepUnit, TimeSince elapsedTimer, Runnable uponTimeout) { return timeout(threadName, timeout, timeoutUnit, sleepInterval, sleepUnit, elapsedTimer::elapsedTime, elapsedTimer::reset, uponTimeout); }
/** * Create a thread that will interrupt the given thread when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @param threadToInterrupt the thread that should be interrupted upon timeout; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread interruptAfterTimeout(String threadName, long timeout, TimeUnit timeoutUnit, TimeSince elapsedTimer, Thread threadToInterrupt) { return timeout(threadName, timeout, timeoutUnit, 100, TimeUnit.MILLISECONDS, elapsedTimer::elapsedTime, elapsedTimer::reset, () -> threadToInterrupt.interrupt()); }
Thread timeoutThread = Threads.timeout(spec.name() + "-connector-output", connectorTimeoutInSeconds, TimeUnit.SECONDS, timeSinceLastRecord,
/** * Create a thread that will call the supplied function when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * <p> * The thread checks the elapsed time every 100 milliseconds. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @param uponTimeout the function to be called when the maximum amount of time has elapsed; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread timeout(String threadName, long timeout, TimeUnit timeoutUnit, TimeSince elapsedTimer, Runnable uponTimeout) { return timeout(threadName, timeout, timeoutUnit, 100, TimeUnit.MILLISECONDS, elapsedTimer::elapsedTime, elapsedTimer::reset, uponTimeout); }
/** * Create a thread that will call the supplied function when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * <p> * The thread checks the elapsed time every 100 milliseconds. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param sleepInterval the amount of time for the new thread to sleep after checking the elapsed time; must be positive * @param sleepUnit the unit for {@code sleepInterval}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @param uponTimeout the function to be called when the maximum amount of time has elapsed; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread timeout(String threadName, long timeout, TimeUnit timeoutUnit, long sleepInterval, TimeUnit sleepUnit, TimeSince elapsedTimer, Runnable uponTimeout) { return timeout(threadName, timeout, timeoutUnit, sleepInterval, sleepUnit, elapsedTimer::elapsedTime, elapsedTimer::reset, uponTimeout); }
/** * Create a thread that will interrupt the given thread when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @param threadToInterrupt the thread that should be interrupted upon timeout; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread interruptAfterTimeout(String threadName, long timeout, TimeUnit timeoutUnit, TimeSince elapsedTimer, Thread threadToInterrupt) { return timeout(threadName, timeout, timeoutUnit, 100, TimeUnit.MILLISECONDS, elapsedTimer::elapsedTime, elapsedTimer::reset, () -> threadToInterrupt.interrupt()); }
Thread timeoutThread = Threads.timeout(spec.name() + "-connector-output", connectorTimeoutInSeconds, TimeUnit.SECONDS, timeSinceLastRecord,