Class SimpleTimeLimiter
- java.lang.Object
- 
- com.google.common.util.concurrent.SimpleTimeLimiter
 
- 
- All Implemented Interfaces:
- TimeLimiter
 
 @GwtIncompatible public final class SimpleTimeLimiter extends java.lang.Object implements TimeLimiter A TimeLimiter that runs method calls in the background using anExecutorService. If the time limit expires for a given method call, the thread running the call will be interrupted.- Since:
- 1.0
- Author:
- Kevin Bourrillion, Jens Nyman
 
- 
- 
Method SummaryAll Methods Static Methods Instance Methods Concrete 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.<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.static SimpleTimeLimitercreate(java.util.concurrent.ExecutorService executor)Creates a TimeLimiter instance using the given executor service to execute method calls.<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.voidrunUninterruptiblyWithTimeout(java.lang.Runnable runnable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit)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.- 
Methods inherited from class java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 - 
Methods inherited from interface com.google.common.util.concurrent.TimeLimitercallUninterruptiblyWithTimeout, callWithTimeout, newProxy, runUninterruptiblyWithTimeout, runWithTimeout
 
- 
 
- 
- 
- 
Method Detail- 
createpublic static SimpleTimeLimiter create(java.util.concurrent.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- the ExecutorService that will execute the method calls on the target objects; for example, a- Executors.newCachedThreadPool().
- Since:
- 22.0
 
 - 
newProxypublic <T> T newProxy(T target, java.lang.Class<T> interfaceType, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit) Description copied from interface:TimeLimiterReturns 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; }- Specified by:
- newProxyin interface- TimeLimiter
- 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
 
 - 
callWithTimeout@CanIgnoreReturnValue public <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 Description copied from interface:TimeLimiterInvokes 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.- Specified by:
- callWithTimeoutin interface- TimeLimiter
- 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
 
 - 
callUninterruptiblyWithTimeout@CanIgnoreReturnValue public <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 Description copied from interface:TimeLimiterInvokes 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 TimeLimiter.callWithTimeout(Callable, long, TimeUnit)is that this method will ignore interrupts on the current thread.- Specified by:
- callUninterruptiblyWithTimeoutin interface- TimeLimiter
- 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
 
 - 
runWithTimeoutpublic void runWithTimeout(java.lang.Runnable runnable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit) throws java.util.concurrent.TimeoutException, java.lang.InterruptedException Description copied from interface:TimeLimiterInvokes 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.- Specified by:
- runWithTimeoutin interface- TimeLimiter
- 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
 
 - 
runUninterruptiblyWithTimeoutpublic void runUninterruptiblyWithTimeout(java.lang.Runnable runnable, long timeoutDuration, java.util.concurrent.TimeUnit timeoutUnit) throws java.util.concurrent.TimeoutException Description copied from interface:TimeLimiterInvokes 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 TimeLimiter.runWithTimeout(Runnable, long, TimeUnit)is that this method will ignore interrupts on the current thread.- Specified by:
- runUninterruptiblyWithTimeoutin interface- TimeLimiter
- 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
 
 
- 
 
-