Interface TimeLimiter
- 
- All Known Implementing Classes:
- FakeTimeLimiter,- SimpleTimeLimiter
 
 @DoNotMock("Use FakeTimeLimiter") @GwtIncompatible public interface TimeLimiter Imposes a time limit on method calls.- Since:
- 1.0
- Author:
- Kevin Bourrillion, Jens Nyman
 
- 
- 
Method SummaryAll Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description <T extends @Nullable java.lang.Object>
 TcallUninterruptiblyWithTimeout(java.util.concurrent.Callable<T> callable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)Invokes a specified Callable, timing out after the specified time limit.default <T extends @Nullable java.lang.Object>
 TcallUninterruptiblyWithTimeout(java.util.concurrent.Callable<T> callable, java.time.Duration timeout)Invokes a specified Callable, timing out after the specified time limit.<T extends @Nullable java.lang.Object>
 TcallWithTimeout(java.util.concurrent.Callable<T> callable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)Invokes a specified Callable, timing out after the specified time limit.default <T extends @Nullable java.lang.Object>
 TcallWithTimeout(java.util.concurrent.Callable<T> callable, java.time.Duration timeout)Invokes a specified Callable, timing out after the specified time limit.<T> TnewProxy(T target, java.lang.Class<T> interfaceType, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)Returns an instance ofinterfaceTypethat delegates all method calls to thetargetobject, enforcing the specified time limit on each call.default <T> TnewProxy(T target, java.lang.Class<T> interfaceType, java.time.Duration timeout)Returns an instance ofinterfaceTypethat delegates all method calls to thetargetobject, enforcing the specified time limit on each call.voidrunUninterruptiblyWithTimeout(java.lang.Runnable runnable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)Invokes a specified Runnable, timing out after the specified time limit.default voidrunUninterruptiblyWithTimeout(java.lang.Runnable runnable, java.time.Duration timeout)Invokes a specified Runnable, timing out after the specified time limit.voidrunWithTimeout(java.lang.Runnable runnable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)Invokes a specified Runnable, timing out after the specified time limit.default voidrunWithTimeout(java.lang.Runnable runnable, java.time.Duration timeout)Invokes a specified Runnable, timing out after the specified time limit.
 
- 
- 
- 
Method Detail- 
newProxy<T> T newProxy(T target, java.lang.Class<T> interfaceType, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit) Returns an instance ofinterfaceTypethat delegates all method calls to thetargetobject, enforcing the specified time limit on each call. This time-limited delegation is also performed for calls toObject.equals(java.lang.Object),Object.hashCode(), andObject.toString().If the target method call finishes before the limit is reached, the return value or exception is propagated to the caller exactly as-is. If, on the other hand, the time limit is reached, the proxy will attempt to abort the call to the target, and will throw an UncheckedTimeoutExceptionto the caller.It is important to note that the primary purpose of the proxy object is to return control to the caller when the timeout elapses; aborting the target method call is of secondary concern. The particular nature and strength of the guarantees made by the proxy is implementation-dependent. However, it is important that each of the methods on the target object behaves appropriately when its thread is interrupted. For example, to return the value of target.someMethod(), but substituteDEFAULT_VALUEif this method call takes over 50 ms, you can use this code:TimeLimiter limiter = . . .; TargetType proxy = limiter.newProxy( target, TargetType.class, 50, TimeUnit.MILLISECONDS); try { return proxy.someMethod(); } catch (UncheckedTimeoutException e) { return DEFAULT_VALUE; }- Parameters:
- target- the object to proxy
- interfaceType- the interface you wish the returned proxy to implement
- timeoutDuration- with timeoutUnit, the maximum length of time that callers are willing to wait on each method call to the proxy
- timeoutUnit- with timeoutDuration, the maximum length of time that callers are willing to wait on each method call to the proxy
- Returns:
- a time-limiting proxy
- Throws:
- java.lang.IllegalArgumentException- if- interfaceTypeis a regular class, enum, or annotation type, rather than an interface
 
 - 
newProxydefault <T> T newProxy(T target, java.lang.Class<T> interfaceType, java.time.Duration timeout) Returns an instance ofinterfaceTypethat delegates all method calls to thetargetobject, enforcing the specified time limit on each call. This time-limited delegation is also performed for calls toObject.equals(java.lang.Object),Object.hashCode(), andObject.toString().If the target method call finishes before the limit is reached, the return value or exception is propagated to the caller exactly as-is. If, on the other hand, the time limit is reached, the proxy will attempt to abort the call to the target, and will throw an UncheckedTimeoutExceptionto the caller.It is important to note that the primary purpose of the proxy object is to return control to the caller when the timeout elapses; aborting the target method call is of secondary concern. The particular nature and strength of the guarantees made by the proxy is implementation-dependent. However, it is important that each of the methods on the target object behaves appropriately when its thread is interrupted. For example, to return the value of target.someMethod(), but substituteDEFAULT_VALUEif this method call takes over 50 ms, you can use this code:TimeLimiter limiter = . . .; TargetType proxy = limiter.newProxy(target, TargetType.class, Duration.ofMillis(50)); try { return proxy.someMethod(); } catch (UncheckedTimeoutException e) { return DEFAULT_VALUE; }- Parameters:
- target- the object to proxy
- interfaceType- the interface you wish the returned proxy to implement
- timeout- the maximum length of time that callers are willing to wait on each method call to the proxy
- Returns:
- a time-limiting proxy
- Throws:
- java.lang.IllegalArgumentException- if- interfaceTypeis a regular class, enum, or annotation type, rather than an interface
- Since:
- 28.0
 
 - 
callWithTimeout@CanIgnoreReturnValue <T extends @Nullable java.lang.Object> T callWithTimeout(java.util.concurrent.Callable<T> callable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException, java.util.concurrent.ExecutionException Invokes a specified Callable, timing out after the specified time limit. If the target method call finishes before the limit is reached, the return value or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to the target, and throw aTimeoutExceptionto the caller.- Parameters:
- callable- the Callable to execute
- timeoutDuration- with timeoutUnit, the maximum length of time to wait
- timeoutUnit- with timeoutDuration, the maximum length of time to wait
- Returns:
- the result returned by the Callable
- Throws:
- java.util.concurrent.TimeoutException- if the time limit is reached
- java.lang.InterruptedException- if the current thread was interrupted during execution
- java.util.concurrent.ExecutionException- if- callablethrows a checked exception
- UncheckedExecutionException- if- callablethrows a- RuntimeException
- ExecutionError- if- callablethrows an- Error
- Since:
- 22.0
 
 - 
callWithTimeout@CanIgnoreReturnValue default <T extends @Nullable java.lang.Object> T callWithTimeout(java.util.concurrent.Callable<T> callable, java.time.Duration timeout) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException, java.util.concurrent.ExecutionException Invokes a specified Callable, timing out after the specified time limit. If the target method call finishes before the limit is reached, the return value or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to the target, and throw aTimeoutExceptionto the caller.- Parameters:
- callable- the Callable to execute
- timeout- the maximum length of time to wait
- Returns:
- the result returned by the Callable
- Throws:
- java.util.concurrent.TimeoutException- if the time limit is reached
- java.lang.InterruptedException- if the current thread was interrupted during execution
- java.util.concurrent.ExecutionException- if- callablethrows a checked exception
- UncheckedExecutionException- if- callablethrows a- RuntimeException
- ExecutionError- if- callablethrows an- Error
- Since:
- 28.0
 
 - 
callUninterruptiblyWithTimeout@CanIgnoreReturnValue <T extends @Nullable java.lang.Object> T callUninterruptiblyWithTimeout(java.util.concurrent.Callable<T> callable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit) throws java.util.concurrent.TimeoutException, java.util.concurrent.ExecutionException Invokes a specified Callable, timing out after the specified time limit. If the target method call finishes before the limit is reached, the return value or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to the target, and throw aTimeoutExceptionto the caller.The difference with callWithTimeout(Callable, long, TimeUnit)is that this method will ignore interrupts on the current thread.- Parameters:
- callable- the Callable to execute
- timeoutDuration- with timeoutUnit, the maximum length of time to wait
- timeoutUnit- with timeoutDuration, the maximum length of time to wait
- Returns:
- the result returned by the Callable
- Throws:
- java.util.concurrent.TimeoutException- if the time limit is reached
- java.util.concurrent.ExecutionException- if- callablethrows a checked exception
- UncheckedExecutionException- if- callablethrows a- RuntimeException
- ExecutionError- if- callablethrows an- Error
- Since:
- 22.0
 
 - 
callUninterruptiblyWithTimeout@CanIgnoreReturnValue default <T extends @Nullable java.lang.Object> T callUninterruptiblyWithTimeout(java.util.concurrent.Callable<T> callable, java.time.Duration timeout) throws java.util.concurrent.TimeoutException, java.util.concurrent.ExecutionException Invokes a specified Callable, timing out after the specified time limit. If the target method call finishes before the limit is reached, the return value or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to the target, and throw aTimeoutExceptionto the caller.The difference with callWithTimeout(Callable, Duration)is that this method will ignore interrupts on the current thread.- Parameters:
- callable- the Callable to execute
- timeout- the maximum length of time to wait
- Returns:
- the result returned by the Callable
- Throws:
- java.util.concurrent.TimeoutException- if the time limit is reached
- java.util.concurrent.ExecutionException- if- callablethrows a checked exception
- UncheckedExecutionException- if- callablethrows a- RuntimeException
- ExecutionError- if- callablethrows an- Error
- Since:
- 28.0
 
 - 
runWithTimeoutvoid runWithTimeout(java.lang.Runnable runnable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException Invokes a specified Runnable, timing out after the specified time limit. If the target method run finishes before the limit is reached, this method returns or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and throw aTimeoutExceptionto the caller.- Parameters:
- runnable- the Runnable to execute
- timeoutDuration- with timeoutUnit, the maximum length of time to wait
- timeoutUnit- with timeoutDuration, the maximum length of time to wait
- Throws:
- java.util.concurrent.TimeoutException- if the time limit is reached
- java.lang.InterruptedException- if the current thread was interrupted during execution
- UncheckedExecutionException- if- runnablethrows a- RuntimeException
- ExecutionError- if- runnablethrows an- Error
- Since:
- 22.0
 
 - 
runWithTimeoutdefault void runWithTimeout(java.lang.Runnable runnable, java.time.Duration timeout) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException Invokes a specified Runnable, timing out after the specified time limit. If the target method run finishes before the limit is reached, this method returns or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and throw aTimeoutExceptionto the caller.- Parameters:
- runnable- the Runnable to execute
- timeout- the maximum length of time to wait
- Throws:
- java.util.concurrent.TimeoutException- if the time limit is reached
- java.lang.InterruptedException- if the current thread was interrupted during execution
- UncheckedExecutionException- if- runnablethrows a- RuntimeException
- ExecutionError- if- runnablethrows an- Error
- Since:
- 28.0
 
 - 
runUninterruptiblyWithTimeoutvoid runUninterruptiblyWithTimeout(java.lang.Runnable runnable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit) throws java.util.concurrent.TimeoutException Invokes a specified Runnable, timing out after the specified time limit. If the target method run finishes before the limit is reached, this method returns or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and throw aTimeoutExceptionto the caller.The difference with runWithTimeout(Runnable, long, TimeUnit)is that this method will ignore interrupts on the current thread.- Parameters:
- runnable- the Runnable to execute
- timeoutDuration- with timeoutUnit, the maximum length of time to wait
- timeoutUnit- with timeoutDuration, the maximum length of time to wait
- Throws:
- java.util.concurrent.TimeoutException- if the time limit is reached
- UncheckedExecutionException- if- runnablethrows a- RuntimeException
- ExecutionError- if- runnablethrows an- Error
- Since:
- 22.0
 
 - 
runUninterruptiblyWithTimeoutdefault void runUninterruptiblyWithTimeout(java.lang.Runnable runnable, java.time.Duration timeout) throws java.util.concurrent.TimeoutException Invokes a specified Runnable, timing out after the specified time limit. If the target method run finishes before the limit is reached, this method returns or a wrapped exception is propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and throw aTimeoutExceptionto the caller.The difference with runWithTimeout(Runnable, Duration)is that this method will ignore interrupts on the current thread.- Parameters:
- runnable- the Runnable to execute
- timeout- the maximum length of time to wait
- Throws:
- java.util.concurrent.TimeoutException- if the time limit is reached
- UncheckedExecutionException- if- runnablethrows a- RuntimeException
- ExecutionError- if- runnablethrows an- Error
- Since:
- 28.0
 
 
- 
 
-