@Beta @GwtIncompatible public interface TimeLimiter
Modifier and Type | Method and Description |
---|---|
<T> T |
callUninterruptiblyWithTimeout(Callable<T> callable,
long timeoutDuration,
TimeUnit timeoutUnit)
Invokes a specified Callable, timing out after the specified time limit.
|
<T> T |
callWithTimeout(Callable<T> callable,
long timeoutDuration,
TimeUnit timeoutUnit)
Invokes a specified Callable, timing out after the specified time limit.
|
<T> T |
callWithTimeout(Callable<T> callable,
long timeoutDuration,
TimeUnit timeoutUnit,
boolean interruptible)
Deprecated.
Use one of the other
call[Uninterruptibly]WithTimeout() or run[Uninterruptibly]WithTimeout() methods. This method is scheduled to be removed in Guava
23.0. |
<T> T |
newProxy(T target,
Class<T> interfaceType,
long timeoutDuration,
TimeUnit timeoutUnit)
Returns an instance of
interfaceType that delegates all method calls to the
target object, enforcing the specified time limit on each call. |
void |
runUninterruptiblyWithTimeout(Runnable runnable,
long timeoutDuration,
TimeUnit timeoutUnit)
Invokes a specified Runnable, timing out after the specified time limit.
|
void |
runWithTimeout(Runnable runnable,
long timeoutDuration,
TimeUnit timeoutUnit)
Invokes a specified Runnable, timing out after the specified time limit.
|
<T> T newProxy(T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit)
interfaceType
that delegates all method calls to the
target
object, enforcing the specified time limit on each call. This time-limited
delegation is also performed for calls to Object.equals(java.lang.Object)
, Object.hashCode()
, and
Object.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
UncheckedTimeoutException
to 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 substitute DEFAULT_VALUE
if 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; }
target
- the object to proxyinterfaceType
- the interface you wish the returned proxy to implementtimeoutDuration
- with timeoutUnit, the maximum length of time that callers are willing to
wait on each method call to the proxytimeoutUnit
- with timeoutDuration, the maximum length of time that callers are willing to
wait on each method call to the proxyIllegalArgumentException
- if interfaceType
is a regular class, enum, or
annotation type, rather than an interface@Deprecated <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit, boolean interruptible) throws Exception
call[Uninterruptibly]WithTimeout()
or run[Uninterruptibly]WithTimeout()
methods. This method is scheduled to be removed in Guava
23.0.UncheckedTimeoutException
to the caller.callable
- the Callable to executetimeoutDuration
- with timeoutUnit, the maximum length of time to waittimeoutUnit
- with timeoutDuration, the maximum length of time to waitinterruptible
- whether to respond to thread interruption by aborting the operation and
throwing InterruptedException; if false, the operation is allowed to complete or time out,
and the current thread's interrupt status is re-asserted.InterruptedException
- if interruptible
is true and our thread is interrupted
during executionUncheckedTimeoutException
- if the time limit is reachedException
<T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException, ExecutionException
TimeoutException
to the caller.callable
- the Callable to executetimeoutDuration
- with timeoutUnit, the maximum length of time to waittimeoutUnit
- with timeoutDuration, the maximum length of time to waitTimeoutException
- if the time limit is reachedInterruptedException
- if the current thread was interrupted during executionExecutionException
- if callable
throws a checked exceptionUncheckedExecutionException
- if callable
throws a RuntimeException
ExecutionError
- if callable
throws an Error
<T> T callUninterruptiblyWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, ExecutionException
TimeoutException
to the caller.
The difference with callWithTimeout(Callable, long, TimeUnit)
is that this method
will ignore interrupts on the current thread.
callable
- the Callable to executetimeoutDuration
- with timeoutUnit, the maximum length of time to waittimeoutUnit
- with timeoutDuration, the maximum length of time to waitTimeoutException
- if the time limit is reachedExecutionException
- if callable
throws a checked exceptionUncheckedExecutionException
- if callable
throws a RuntimeException
ExecutionError
- if callable
throws an Error
void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException
TimeoutException
to the caller.runnable
- the Runnable to executetimeoutDuration
- with timeoutUnit, the maximum length of time to waittimeoutUnit
- with timeoutDuration, the maximum length of time to waitTimeoutException
- if the time limit is reachedInterruptedException
- if the current thread was interrupted during executionUncheckedExecutionException
- if runnable
throws a RuntimeException
ExecutionError
- if runnable
throws an Error
void runUninterruptiblyWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException
TimeoutException
to the caller.
The difference with runWithTimeout(Runnable, long, TimeUnit)
is that this method
will ignore interrupts on the current thread.
runnable
- the Runnable to executetimeoutDuration
- with timeoutUnit, the maximum length of time to waittimeoutUnit
- with timeoutDuration, the maximum length of time to waitTimeoutException
- if the time limit is reachedUncheckedExecutionException
- if runnable
throws a RuntimeException
ExecutionError
- if runnable
throws an Error
Copyright © 2010-2017. All Rights Reserved.