Package io.github.connellite.concurrent
Class ConcurrentUtils
java.lang.Object
io.github.connellite.concurrent.ConcurrentUtils
Small helpers for running work with a time limit on a dedicated worker thread.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voidrunWithTimeout(Runnable operation, long timeout, TimeUnit unit) Runsoperationon a worker thread and waits up totimeoutinunit.static voidrunWithTimeout(Runnable operation, long timeout, TimeUnit unit, String operationName) Runsoperationon a worker thread and waits up totimeoutinunit.static <T> TrunWithTimeout(Callable<T> operation, long timeout, TimeUnit unit) Runsoperationon a worker thread and waits up totimeoutinunit.static <T> TrunWithTimeout(Callable<T> operation, long timeout, TimeUnit unit, String operationName) Runsoperationon a worker thread and waits up totimeoutinunit.
-
Constructor Details
-
ConcurrentUtils
public ConcurrentUtils()
-
-
Method Details
-
runWithTimeout
public static <T> T runWithTimeout(Callable<T> operation, long timeout, TimeUnit unit) throws Exception Runsoperationon a worker thread and waits up totimeoutinunit.- Parameters:
operation- callable to execute; must not benulltimeout- maximum wait time; must be non-negativeunit- time unit fortimeout; must not benull- Returns:
- result of
operation - Throws:
Exception- ifoperationfails or the wait is interrupted
-
runWithTimeout
public static <T> T runWithTimeout(Callable<T> operation, long timeout, TimeUnit unit, String operationName) throws Exception Runsoperationon a worker thread and waits up totimeoutinunit.- Parameters:
operation- callable to execute; must not benulltimeout- maximum wait time; must be non-negativeunit- time unit fortimeout; must not benulloperationName- optional label used in timeout messages; may benull- Returns:
- result of
operation - Throws:
Exception- ifoperationfails or the wait is interrupted
-
runWithTimeout
Runsoperationon a worker thread and waits up totimeoutinunit.- Parameters:
operation- runnable to execute; must not benulltimeout- maximum wait time; must be non-negativeunit- time unit fortimeout; must not benull- Throws:
Exception- ifoperationfails or the wait is interrupted
-
runWithTimeout
public static void runWithTimeout(Runnable operation, long timeout, TimeUnit unit, String operationName) throws Exception Runsoperationon a worker thread and waits up totimeoutinunit.- Parameters:
operation- runnable to execute; must not benulltimeout- maximum wait time; must be non-negativeunit- time unit fortimeout; must not benulloperationName- optional label used in timeout messages; may benull- Throws:
Exception- ifoperationfails or the wait is interrupted
-