Package fr.dyade.aaa.common.concurrent
Class TimeLimiter
- java.lang.Object
-
- fr.dyade.aaa.common.concurrent.TimeLimiter
-
public class TimeLimiter extends Object
Imposes a time limit on method calls.
-
-
Constructor Summary
Constructors Constructor Description TimeLimiter()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description static <T> TcallWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit)Invokes a specified Callable with the specified time limit.voidrunWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit)Invokes a specified Runnable with the specified time limit.
-
-
-
Method Detail
-
callWithTimeout
public static <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws Exception
Invokes a specified Callable with the specified time limit. If the target method call finishes before the limit is reached, the return value or the exception is propagated. If the time limit is reached, we attempt to abort the call, and throw aTimeoutExceptionto the caller.- Type Parameters:
T- the type of the task's result- Parameters:
callable- the Callable to executetimeoutDuration- with timeoutUnit, the maximum length of time to waittimeoutUnit- with timeoutDuration, the maximum length of time to wait- Returns:
- the result returned by the Callable
- Throws:
Exception- if the time limit is reached, the thread is interrupted, or an exception is thrown during execution.
-
runWithTimeout
public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws Exception
Invokes a specified Runnable with the specified time limit. If the target method call finishes before the limit is reached, the return value or the exception is propagated. If the time limit is reached, we attempt to abort the call, and throw aTimeoutExceptionto the caller.- Parameters:
runnable- the Runnable to executetimeoutDuration- with timeoutUnit, the maximum length of time to waittimeoutUnit- with timeoutDuration, the maximum length of time to wait- Throws:
Exception- if the time limit is reached, the thread is interrupted, or an exception is thrown during execution.
-
-