ExecutorService. If the
time limit expires for a given method call, the thread running the call will be interrupted.
| Modifier and Type | Field and Description |
|---|---|
| private final ExecutorService |
| Access | Constructor and Description |
|---|---|
| private |
| Modifier and Type | Method 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. |
| 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. |
| private static void | |
| public static SimpleTimeLimiter | create(ExecutorService
the ExecutorService that will execute the method calls on the target objects;
for example, a executor)Executors#newCachedThreadPool().Creates a TimeLimiter instance using the given executor service to execute method calls. |
| private static boolean | |
| private static Set | |
| 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. interfaceType that delegates all method calls to the target object, enforcing the specified time limit on each call.
|
| private static <T> T | |
| 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. |
| 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. |
| private static Exception | |
| private void | |
| private void |
| executor | back to summary |
|---|---|
| private final ExecutorService executor | |
| SimpleTimeLimiter | back to summary |
|---|---|
| private SimpleTimeLimiter(ExecutorService executor) | |
| callUninterruptiblyWithTimeout | back to summary |
|---|---|
| public <T extends @Nullable Object> T callUninterruptiblyWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, ExecutionException Implements com. Doc from com. 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 The difference with
| |
| callWithTimeout | back to summary |
|---|---|
private <T extends @Nullable Object> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit, boolean amInterruptible) throws Exception
| |
| callWithTimeout | back to summary |
|---|---|
| public <T extends @Nullable Object> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException, ExecutionException Implements com. Doc from com. 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
| |
| checkPositiveTimeout | back to summary |
|---|---|
| private static void checkPositiveTimeout(long timeoutDuration) | |
| create | back 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.
| |
| declaresInterruptedEx | back to summary |
|---|---|
| private static boolean declaresInterruptedEx(Method method) | |
| findInterruptibleMethods | back to summary |
|---|---|
| private static Set | |
| newProxy | back to summary |
|---|---|
| public <T> T newProxy(T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit) Implements com. Doc from com. Returns an instance of 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 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
TimeLimiter limiter = . . .;
TargetType proxy = limiter.newProxy(
target, TargetType.class, 50, TimeUnit.MILLISECONDS);
try {
return proxy.someMethod();
} catch (UncheckedTimeoutException e) {
return DEFAULT_VALUE;
}
| |
| newProxy | back to summary |
|---|---|
| private static <T> T newProxy(Class<T> interfaceType, InvocationHandler handler) | |
| runUninterruptiblyWithTimeout | back to summary |
|---|---|
| public void runUninterruptiblyWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException Implements com. Doc from com. 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 The difference with
| |
| runWithTimeout | back to summary |
|---|---|
| public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, InterruptedException Implements com. Doc from com. 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
| |
| throwCause | back to summary |
|---|---|
| private static Exception throwCause(Exception e, boolean combineStackTraces) throws Exception | |
| wrapAndThrowExecutionExceptionOrError | back to summary |
|---|---|
| private void wrapAndThrowExecutionExceptionOrError(Throwable cause) throws ExecutionException | |
| wrapAndThrowRuntimeExecutionExceptionOrError | back to summary |
|---|---|
| private void wrapAndThrowRuntimeExecutionExceptionOrError(Throwable cause) | |