Top Description Methods
com.google.common.util.concurrent

public Interface TimeLimiter

Known Direct Implementers
com.google.common.util.concurrent.FakeTimeLimiter, com.google.common.util.concurrent.SimpleTimeLimiter
Annotations
@DoNotMock:Use FakeTimeLimiter
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.util.concurrent.Internal.toNanosSaturated

Imposes a time limit on method calls.
Authors
Kevin Bourrillion, Jens Nyman
Since
1.0

Method Summary

Modifier and TypeMethod and Description
public <T extends @Nullable Object> T

Returns:

the result returned by the Callable
callUninterruptiblyWithTimeout
(Callable<T>
the Callable to execute
callable
,
long
with timeoutUnit, the maximum length of time to wait
timeoutDuration
,
TimeUnit
with timeoutDuration, the maximum length of time to wait
timeoutUnit
)

Invokes a specified Callable, timing out after the specified time limit.

public default <T extends @Nullable Object> T

Returns:

the result returned by the Callable
callUninterruptiblyWithTimeout
(Callable<T>
the Callable to execute
callable
,
Duration
the maximum length of time to wait
timeout
)

Invokes a specified Callable, timing out after the specified time limit.

public <T extends @Nullable Object> T

Returns:

the result returned by the Callable
callWithTimeout
(Callable<T>
the Callable to execute
callable
,
long
with timeoutUnit, the maximum length of time to wait
timeoutDuration
,
TimeUnit
with timeoutDuration, the maximum length of time to wait
timeoutUnit
)

Invokes a specified Callable, timing out after the specified time limit.

public default <T extends @Nullable Object> T

Returns:

the result returned by the Callable
callWithTimeout
(Callable<T>
the Callable to execute
callable
,
Duration
the maximum length of time to wait
timeout
)

Invokes a specified Callable, timing out after the specified time limit.

public <T> T

Returns:

a time-limiting proxy
newProxy
(T
the object to proxy
target
,
Class<T>
the interface you wish the returned proxy to implement
interfaceType
,
long
with timeoutUnit, the maximum length of time that callers are willing to wait on each method call to the proxy
timeoutDuration
,
TimeUnit
with timeoutDuration, the maximum length of time that callers are willing to wait on each method call to the proxy
timeoutUnit
)

Returns an instance of interfaceType that delegates all method calls to the target object, enforcing the specified time limit on each call.

public default <T> T

Returns:

a time-limiting proxy
newProxy
(T
the object to proxy
target
,
Class<T>
the interface you wish the returned proxy to implement
interfaceType
,
Duration
the maximum length of time that callers are willing to wait on each method call to the proxy
timeout
)

Returns an instance of interfaceType that delegates all method calls to the target object, enforcing the specified time limit on each call.

public void
runUninterruptiblyWithTimeout(Runnable
the Runnable to execute
runnable
,
long
with timeoutUnit, the maximum length of time to wait
timeoutDuration
,
TimeUnit
with timeoutDuration, the maximum length of time to wait
timeoutUnit
)

Invokes a specified Runnable, timing out after the specified time limit.

public default void
runUninterruptiblyWithTimeout(Runnable
the Runnable to execute
runnable
,
Duration
the maximum length of time to wait
timeout
)

Invokes a specified Runnable, timing out after the specified time limit.

public void
runWithTimeout(Runnable
the Runnable to execute
runnable
,
long
with timeoutUnit, the maximum length of time to wait
timeoutDuration
,
TimeUnit
with timeoutDuration, the maximum length of time to wait
timeoutUnit
)

Invokes a specified Runnable, timing out after the specified time limit.

public default void
runWithTimeout(Runnable
the Runnable to execute
runnable
,
Duration
the maximum length of time to wait
timeout
)

Invokes a specified Runnable, timing out after the specified time limit.

Method Detail

callUninterruptiblyWithTimeoutback to summary
public <T extends @Nullable Object> T callUninterruptiblyWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, 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 a TimeoutException to the caller.

The difference with callWithTimeout(Callable, long, TimeUnit) is that this method will ignore interrupts on the current thread.

Parameters
callable:Callable<T>

the Callable to execute

timeoutDuration:long

with timeoutUnit, the maximum length of time to wait

timeoutUnit:TimeUnit

with timeoutDuration, the maximum length of time to wait

Returns:T

the result returned by the Callable

Annotations
@SuppressWarnings:GoodTime
@CanIgnoreReturnValue
@ParametricNullness
Exceptions
TimeoutException:
if the time limit is reached
ExecutionException:
if callable throws a checked exception
UncheckedExecutionException:
if callable throws a RuntimeException
ExecutionError:
if callable throws an Error
Since
22.0
callUninterruptiblyWithTimeoutback to summary
public default <T extends @Nullable Object> T callUninterruptiblyWithTimeout(Callable<T> callable, Duration timeout) throws TimeoutException, 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 a TimeoutException to the caller.

The difference with callWithTimeout(Callable, Duration) is that this method will ignore interrupts on the current thread.

Parameters
callable:Callable<T>

the Callable to execute

timeout:Duration

the maximum length of time to wait

Returns:T

the result returned by the Callable

Annotations
@CanIgnoreReturnValue
@ParametricNullness
Exceptions
TimeoutException:
if the time limit is reached
ExecutionException:
if callable throws a checked exception
UncheckedExecutionException:
if callable throws a RuntimeException
ExecutionError:
if callable throws an Error
Since
28.0
callWithTimeoutback to summary
public <T extends @Nullable Object> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException, 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 a TimeoutException to the caller.

Parameters
callable:Callable<T>

the Callable to execute

timeoutDuration:long

with timeoutUnit, the maximum length of time to wait

timeoutUnit:TimeUnit

with timeoutDuration, the maximum length of time to wait

Returns:T

the result returned by the Callable

Annotations
@SuppressWarnings:GoodTime
@CanIgnoreReturnValue
@ParametricNullness
Exceptions
TimeoutException:
if the time limit is reached
InterruptedException:
if the current thread was interrupted during execution
ExecutionException:
if callable throws a checked exception
UncheckedExecutionException:
if callable throws a RuntimeException
ExecutionError:
if callable throws an Error
Since
22.0
callWithTimeoutback to summary
public default <T extends @Nullable Object> T callWithTimeout(Callable<T> callable, Duration timeout) throws TimeoutException, InterruptedException, 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 a TimeoutException to the caller.

Parameters
callable:Callable<T>

the Callable to execute

timeout:Duration

the maximum length of time to wait

Returns:T

the result returned by the Callable

Annotations
@CanIgnoreReturnValue
@ParametricNullness
Exceptions
TimeoutException:
if the time limit is reached
InterruptedException:
if the current thread was interrupted during execution
ExecutionException:
if callable throws a checked exception
UncheckedExecutionException:
if callable throws a RuntimeException
ExecutionError:
if callable throws an Error
Since
28.0
newProxyback to summary
public <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. This time-limited delegation is also performed for calls to Object#equals, 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;
  }
Parameters
target:T

the object to proxy

interfaceType:Class<T>

the interface you wish the returned proxy to implement

timeoutDuration:long

with timeoutUnit, the maximum length of time that callers are willing to wait on each method call to the proxy

timeoutUnit:TimeUnit

with timeoutDuration, the maximum length of time that callers are willing to wait on each method call to the proxy

Returns:T

a time-limiting proxy

Annotations
@SuppressWarnings:GoodTime
Exceptions
IllegalArgumentException:
if interfaceType is a regular class, enum, or annotation type, rather than an interface
newProxyback to summary
public default <T> T newProxy(T target, Class<T> interfaceType, Duration timeout)

Returns an instance of 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, 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, Duration.ofMillis(50));
  try {
    return proxy.someMethod();
  } catch (UncheckedTimeoutException e) {
    return DEFAULT_VALUE;
  }
Parameters
target:T

the object to proxy

interfaceType:Class<T>

the interface you wish the returned proxy to implement

timeout:Duration

the maximum length of time that callers are willing to wait on each method call to the proxy

Returns:T

a time-limiting proxy

Exceptions
IllegalArgumentException:
if interfaceType is a regular class, enum, or annotation type, rather than an interface
Since
28.0
runUninterruptiblyWithTimeoutback to summary
public void runUninterruptiblyWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws 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 a TimeoutException to the caller.

The difference with runWithTimeout(Runnable, long, TimeUnit) is that this method will ignore interrupts on the current thread.

Parameters
runnable:Runnable

the Runnable to execute

timeoutDuration:long

with timeoutUnit, the maximum length of time to wait

timeoutUnit:TimeUnit

with timeoutDuration, the maximum length of time to wait

Annotations
@SuppressWarnings:GoodTime
Exceptions
TimeoutException:
if the time limit is reached
UncheckedExecutionException:
if runnable throws a RuntimeException
ExecutionError:
if runnable throws an Error
Since
22.0
runUninterruptiblyWithTimeoutback to summary
public default void runUninterruptiblyWithTimeout(Runnable runnable, Duration timeout) throws 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 a TimeoutException to the caller.

The difference with runWithTimeout(Runnable, Duration) is that this method will ignore interrupts on the current thread.

Parameters
runnable:Runnable

the Runnable to execute

timeout:Duration

the maximum length of time to wait

Exceptions
TimeoutException:
if the time limit is reached
UncheckedExecutionException:
if runnable throws a RuntimeException
ExecutionError:
if runnable throws an Error
Since
28.0
runWithTimeoutback to summary
public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, 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 a TimeoutException to the caller.

Parameters
runnable:Runnable

the Runnable to execute

timeoutDuration:long

with timeoutUnit, the maximum length of time to wait

timeoutUnit:TimeUnit

with timeoutDuration, the maximum length of time to wait

Annotations
@SuppressWarnings:GoodTime
Exceptions
TimeoutException:
if the time limit is reached
InterruptedException:
if the current thread was interrupted during execution
UncheckedExecutionException:
if runnable throws a RuntimeException
ExecutionError:
if runnable throws an Error
Since
22.0
runWithTimeoutback to summary
public default void runWithTimeout(Runnable runnable, Duration timeout) throws TimeoutException, 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 a TimeoutException to the caller.

Parameters
runnable:Runnable

the Runnable to execute

timeout:Duration

the maximum length of time to wait

Exceptions
TimeoutException:
if the time limit is reached
InterruptedException:
if the current thread was interrupted during execution
UncheckedExecutionException:
if runnable throws a RuntimeException
ExecutionError:
if runnable throws an Error
Since
28.0