001/* 002 * Copyright (C) 2006 The Guava Authors 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 005 * in compliance with the License. You may obtain a copy of the License at 006 * 007 * http://www.apache.org/licenses/LICENSE-2.0 008 * 009 * Unless required by applicable law or agreed to in writing, software distributed under the License 010 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 011 * or implied. See the License for the specific language governing permissions and limitations under 012 * the License. 013 */ 014 015package com.google.common.util.concurrent; 016 017import com.google.common.annotations.Beta; 018import com.google.common.annotations.GwtIncompatible; 019import java.util.concurrent.Callable; 020import java.util.concurrent.ExecutionException; 021import java.util.concurrent.TimeUnit; 022import java.util.concurrent.TimeoutException; 023 024/** 025 * Imposes a time limit on method calls. 026 * 027 * @author Kevin Bourrillion 028 * @author Jens Nyman 029 * @since 1.0 030 */ 031@Beta 032@GwtIncompatible 033public interface TimeLimiter { 034 035 /** 036 * Returns an instance of {@code interfaceType} that delegates all method calls to the 037 * {@code target} object, enforcing the specified time limit on each call. This time-limited 038 * delegation is also performed for calls to {@link Object#equals}, {@link Object#hashCode}, and 039 * {@link Object#toString}. 040 * 041 * <p>If the target method call finishes before the limit is reached, the return value or 042 * exception is propagated to the caller exactly as-is. If, on the other hand, the time limit is 043 * reached, the proxy will attempt to abort the call to the target, and will throw an 044 * {@link UncheckedTimeoutException} to the caller. 045 * 046 * <p>It is important to note that the primary purpose of the proxy object is to return control to 047 * the caller when the timeout elapses; aborting the target method call is of secondary concern. 048 * The particular nature and strength of the guarantees made by the proxy is 049 * implementation-dependent. However, it is important that each of the methods on the target 050 * object behaves appropriately when its thread is interrupted. 051 * 052 * <p>For example, to return the value of {@code target.someMethod()}, but substitute {@code 053 * DEFAULT_VALUE} if this method call takes over 50 ms, you can use this code: 054 * 055 * <pre> 056 * TimeLimiter limiter = . . .; 057 * TargetType proxy = limiter.newProxy( 058 * target, TargetType.class, 50, TimeUnit.MILLISECONDS); 059 * try { 060 * return proxy.someMethod(); 061 * } catch (UncheckedTimeoutException e) { 062 * return DEFAULT_VALUE; 063 * } 064 * </pre> 065 * 066 * @param target the object to proxy 067 * @param interfaceType the interface you wish the returned proxy to implement 068 * @param timeoutDuration with timeoutUnit, the maximum length of time that callers are willing to 069 * wait on each method call to the proxy 070 * @param timeoutUnit with timeoutDuration, the maximum length of time that callers are willing to 071 * wait on each method call to the proxy 072 * @return a time-limiting proxy 073 * @throws IllegalArgumentException if {@code interfaceType} is a regular class, enum, or 074 * annotation type, rather than an interface 075 */ 076 <T> T newProxy(T target, Class<T> interfaceType, long timeoutDuration, TimeUnit timeoutUnit); 077 078 /** 079 * Invokes a specified Callable, timing out after the specified time limit. If the target method 080 * call finishes before the limit is reached, the return value or a wrapped exception is 081 * propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to 082 * the target, and throw a {@link TimeoutException} to the caller. 083 * 084 * @param callable the Callable to execute 085 * @param timeoutDuration with timeoutUnit, the maximum length of time to wait 086 * @param timeoutUnit with timeoutDuration, the maximum length of time to wait 087 * @return the result returned by the Callable 088 * @throws TimeoutException if the time limit is reached 089 * @throws InterruptedException if the current thread was interrupted during execution 090 * @throws ExecutionException if {@code callable} throws a checked exception 091 * @throws UncheckedExecutionException if {@code callable} throws a {@code RuntimeException} 092 * @throws ExecutionError if {@code callable} throws an {@code Error} 093 * @since 22.0 094 */ 095 <T> T callWithTimeout(Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) 096 throws TimeoutException, InterruptedException, ExecutionException; 097 098 /** 099 * Invokes a specified Callable, timing out after the specified time limit. If the target method 100 * call finishes before the limit is reached, the return value or a wrapped exception is 101 * propagated. If, on the other hand, the time limit is reached, we attempt to abort the call to 102 * the target, and throw a {@link TimeoutException} to the caller. 103 * 104 * <p>The difference with {@link #callWithTimeout(Callable, long, TimeUnit)} is that this method 105 * will ignore interrupts on the current thread. 106 * 107 * @param callable the Callable to execute 108 * @param timeoutDuration with timeoutUnit, the maximum length of time to wait 109 * @param timeoutUnit with timeoutDuration, the maximum length of time to wait 110 * @return the result returned by the Callable 111 * @throws TimeoutException if the time limit is reached 112 * @throws ExecutionException if {@code callable} throws a checked exception 113 * @throws UncheckedExecutionException if {@code callable} throws a {@code RuntimeException} 114 * @throws ExecutionError if {@code callable} throws an {@code Error} 115 * @since 22.0 116 */ 117 <T> T callUninterruptiblyWithTimeout( 118 Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) 119 throws TimeoutException, ExecutionException; 120 121 /** 122 * Invokes a specified Runnable, timing out after the specified time limit. If the target method 123 * run finishes before the limit is reached, this method returns or a wrapped exception is 124 * propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and 125 * throw a {@link TimeoutException} to the caller. 126 * 127 * @param runnable the Runnable to execute 128 * @param timeoutDuration with timeoutUnit, the maximum length of time to wait 129 * @param timeoutUnit with timeoutDuration, the maximum length of time to wait 130 * @throws TimeoutException if the time limit is reached 131 * @throws InterruptedException if the current thread was interrupted during execution 132 * @throws UncheckedExecutionException if {@code runnable} throws a {@code RuntimeException} 133 * @throws ExecutionError if {@code runnable} throws an {@code Error} 134 * @since 22.0 135 */ 136 void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) 137 throws TimeoutException, InterruptedException; 138 139 /** 140 * Invokes a specified Runnable, timing out after the specified time limit. If the target method 141 * run finishes before the limit is reached, this method returns or a wrapped exception is 142 * propagated. If, on the other hand, the time limit is reached, we attempt to abort the run, and 143 * throw a {@link TimeoutException} to the caller. 144 * 145 * <p>The difference with {@link #runWithTimeout(Runnable, long, TimeUnit)} is that this method 146 * will ignore interrupts on the current thread. 147 * 148 * @param runnable the Runnable to execute 149 * @param timeoutDuration with timeoutUnit, the maximum length of time to wait 150 * @param timeoutUnit with timeoutDuration, the maximum length of time to wait 151 * @throws TimeoutException if the time limit is reached 152 * @throws UncheckedExecutionException if {@code runnable} throws a {@code RuntimeException} 153 * @throws ExecutionError if {@code runnable} throws an {@code Error} 154 * @since 22.0 155 */ 156 void runUninterruptiblyWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) 157 throws TimeoutException; 158}