@Beta @GwtIncompatible public final class SimpleTimeLimiter extends Object implements TimeLimiter
ExecutorService
. If the
time limit expires for a given method call, the thread running the call will be interrupted.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.
|
static SimpleTimeLimiter |
create(ExecutorService executor)
Creates a TimeLimiter instance using the given executor service to execute method calls.
|
<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.
|
public static SimpleTimeLimiter create(ExecutorService executor)
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.
executor
- the ExecutorService that will execute the method calls on the target objects;
for example, a Executors.newCachedThreadPool()
.public <T> T newProxy(T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit)
TimeLimiter
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; }
newProxy
in interface TimeLimiter
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 proxy@CanIgnoreReturnValue public <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException, ExecutionException
TimeLimiter
TimeoutException
to the caller.callWithTimeout
in interface TimeLimiter
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 exception@CanIgnoreReturnValue public <T> T callUninterruptiblyWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, ExecutionException
TimeLimiter
TimeoutException
to the caller.
The difference with TimeLimiter.callWithTimeout(Callable, long, TimeUnit)
is that this method
will ignore interrupts on the current thread.
callUninterruptiblyWithTimeout
in interface TimeLimiter
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 exceptionpublic void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException
TimeLimiter
TimeoutException
to the caller.runWithTimeout
in interface TimeLimiter
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 executionpublic void runUninterruptiblyWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException
TimeLimiter
TimeoutException
to the caller.
The difference with TimeLimiter.runWithTimeout(Runnable, long, TimeUnit)
is that this method
will ignore interrupts on the current thread.
runUninterruptiblyWithTimeout
in interface TimeLimiter
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 reachedCopyright © 2010–2017. All rights reserved.