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

public final Class SimpleTimeLimiter

extends Object
implements TimeLimiter
Class Inheritance
All Implemented Interfaces
com.google.common.util.concurrent.TimeLimiter
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, com.google.common.util.concurrent.Uninterruptibles.getUninterruptibly

A TimeLimiter that runs method calls in the background using an ExecutorService. If the time limit expires for a given method call, the thread running the call will be interrupted.
Authors
Kevin Bourrillion, Jens Nyman
Since
1.0

Field Summary

Modifier and TypeField and Description
private final ExecutorService

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public <T extends @Nullable Object> T
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
)

Implements com.google.common.util.concurrent.TimeLimiter.callUninterruptiblyWithTimeout.

Invokes a specified Callable, timing out after the specified time limit.
private <T extends @Nullable Object> T
callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit, boolean amInterruptible)

public <T extends @Nullable Object> T
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
)

Implements com.google.common.util.concurrent.TimeLimiter.callWithTimeout.

Invokes a specified Callable, timing out after the specified time limit.
private static void
checkPositiveTimeout(long timeoutDuration)

public static SimpleTimeLimiter
create(ExecutorService
the ExecutorService that will execute the method calls on the target objects; for example, a Executors#newCachedThreadPool().
executor
)

Creates a TimeLimiter instance using the given executor service to execute method calls.

private static boolean
private static Set<Method>
findInterruptibleMethods(Class<?> interfaceType)

public <T> T
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
)

Implements com.google.common.util.concurrent.TimeLimiter.newProxy.

Returns an instance of interfaceType that delegates all method calls to the target object, enforcing the specified time limit on each call.
private static <T> T
newProxy(Class<T> interfaceType, InvocationHandler handler)

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
)

Implements com.google.common.util.concurrent.TimeLimiter.runUninterruptiblyWithTimeout.

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
)

Implements com.google.common.util.concurrent.TimeLimiter.runWithTimeout.

Invokes a specified Runnable, timing out after the specified time limit.
private static Exception
throwCause(Exception e, boolean combineStackTraces)

private void
private void
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

executorback to summary
private final ExecutorService executor

Constructor Detail

SimpleTimeLimiterback to summary
private SimpleTimeLimiter(ExecutorService executor)

Method Detail

callUninterruptiblyWithTimeoutback to summary
public <T extends @Nullable Object> T callUninterruptiblyWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, ExecutionException

Implements com.google.common.util.concurrent.TimeLimiter.callUninterruptiblyWithTimeout.

Doc from com.google.common.util.concurrent.TimeLimiter.callUninterruptiblyWithTimeout.

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
@CanIgnoreReturnValue
@Override
@ParametricNullness
Exceptions
TimeoutException:
if the time limit is reached
ExecutionException:
if callable throws a checked exception
callWithTimeoutback to summary
private <T extends @Nullable Object> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit, boolean amInterruptible) throws Exception
Annotations
@ParametricNullness
callWithTimeoutback to summary
public <T extends @Nullable Object> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException, ExecutionException

Implements com.google.common.util.concurrent.TimeLimiter.callWithTimeout.

Doc from com.google.common.util.concurrent.TimeLimiter.callWithTimeout.

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
@CanIgnoreReturnValue
@Override
@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
checkPositiveTimeoutback to summary
private static void checkPositiveTimeout(long timeoutDuration)
createback to summary
public static SimpleTimeLimiter create(ExecutorService executor)

Creates a TimeLimiter instance using the given executor service to execute method calls.

Warning

using a bounded executor may be counterproductive! If the thread pool fills up, any time callers spend waiting for a thread may count toward their time limit, and in this case the call may even time out before the target method is ever invoked.

Parameters
executor:ExecutorService

the ExecutorService that will execute the method calls on the target objects; for example, a Executors#newCachedThreadPool().

Since
22.0
declaresInterruptedExback to summary
private static boolean declaresInterruptedEx(Method method)
findInterruptibleMethodsback to summary
private static Set<Method> findInterruptibleMethods(Class<?> interfaceType)
newProxyback to summary
public <T> T newProxy(T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit)

Implements com.google.common.util.concurrent.TimeLimiter.newProxy.

Doc from com.google.common.util.concurrent.TimeLimiter.newProxy.

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
@Override
newProxyback to summary
private static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler)
runUninterruptiblyWithTimeoutback to summary
public void runUninterruptiblyWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException

Implements com.google.common.util.concurrent.TimeLimiter.runUninterruptiblyWithTimeout.

Doc from com.google.common.util.concurrent.TimeLimiter.runUninterruptiblyWithTimeout.

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
@Override
Exceptions
TimeoutException:
if the time limit is reached
runWithTimeoutback to summary
public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException

Implements com.google.common.util.concurrent.TimeLimiter.runWithTimeout.

Doc from com.google.common.util.concurrent.TimeLimiter.runWithTimeout.

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
@Override
Exceptions
TimeoutException:
if the time limit is reached
InterruptedException:
if the current thread was interrupted during execution
throwCauseback to summary
private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception
wrapAndThrowExecutionExceptionOrErrorback to summary
private void wrapAndThrowExecutionExceptionOrError(Throwable cause) throws ExecutionException
wrapAndThrowRuntimeExecutionExceptionOrErrorback to summary
private void wrapAndThrowRuntimeExecutionExceptionOrError(Throwable cause)