@Override public V waitForValue() throws ExecutionException { return getUninterruptibly(futureValue); }
@Override public void run() { try { /* * Threads from our private pool are never interrupted. Threads from a * user-supplied executor might be, but... what can we do? This is another reason * to return a proper ListenableFuture instead of using listenInPoolThread. */ getUninterruptibly(delegate); } catch (Throwable e) { // ExecutionException / CancellationException / RuntimeException / Error // The task is presumably done, run the listeners. } executionList.execute(); } });
@Override public void runUninterruptiblyWithTimeout( Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException { checkNotNull(runnable); checkNotNull(timeoutUnit); checkPositiveTimeout(timeoutDuration); Future<?> future = executor.submit(runnable); try { Uninterruptibles.getUninterruptibly(future, timeoutDuration, timeoutUnit); } catch (TimeoutException e) { future.cancel(true /* mayInterruptIfRunning */); throw e; } catch (ExecutionException e) { wrapAndThrowRuntimeExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } }
@Override public Boolean call() throws Exception { assertEquals(RESULT, getUninterruptibly(future, 10, MINUTES)); return Thread.interrupted(); } });
@CanIgnoreReturnValue @Override public <T> T callUninterruptiblyWithTimeout( Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit) throws TimeoutException, ExecutionException { checkNotNull(callable); checkNotNull(timeoutUnit); checkPositiveTimeout(timeoutDuration); Future<T> future = executor.submit(callable); try { return Uninterruptibles.getUninterruptibly(future, timeoutDuration, timeoutUnit); } catch (TimeoutException e) { future.cancel(true /* mayInterruptIfRunning */); throw e; } catch (ExecutionException e) { wrapAndThrowExecutionExceptionOrError(e.getCause()); throw new AssertionError(); } }
@Override public Boolean call() throws Exception { Object actual; if (allowInterruption) { actual = future.get(); } else { actual = getUninterruptibly(future); } assertEquals(RESULT, actual); return Thread.interrupted(); } });
private <T> T callWithTimeout( Callable<T> callable, long timeoutDuration, TimeUnit timeoutUnit, boolean amInterruptible) throws Exception { checkNotNull(callable); checkNotNull(timeoutUnit); checkPositiveTimeout(timeoutDuration); Future<T> future = executor.submit(callable); try { if (amInterruptible) { try { return future.get(timeoutDuration, timeoutUnit); } catch (InterruptedException e) { future.cancel(true); throw e; } } else { return Uninterruptibles.getUninterruptibly(future, timeoutDuration, timeoutUnit); } } catch (ExecutionException e) { throw throwCause(e, true /* combineStackTraces */); } catch (TimeoutException e) { future.cancel(true); throw new UncheckedTimeoutException(e); } }
@Override public void run() { try { String result = Uninterruptibles.getUninterruptibly(currentFuture.get()); finalResults.add(result); } catch (ExecutionException e) { finalResults.add(e.getCause()); } catch (CancellationException e) { finalResults.add(CancellationException.class); } finally { awaitUnchecked(barrier); } } };
@Override public void run() { try { String result = Uninterruptibles.getUninterruptibly(currentFuture.get()); finalResults.add(result); } catch (ExecutionException e) { finalResults.add(e.getCause()); } catch (CancellationException e) { finalResults.add(CancellationException.class); } finally { awaitUnchecked(barrier); } } };
@Override public void run() { try { String result = Uninterruptibles.getUninterruptibly(currentFuture.get()); finalResults.add(result); } catch (ExecutionException e) { finalResults.add(e.getCause()); } catch (CancellationException e) { finalResults.add(CancellationException.class); } finally { awaitUnchecked(barrier); } } };
public void testMakeUninterruptible_timedGetZeroTimeoutAttempted() throws TimeoutException, ExecutionException { SettableFuture<String> future = SettableFuture.create(); future.set(RESULT); /* * getUninterruptibly should call the timed get method once with a * wait of 0 seconds (and it should succeed, since the result is already * available). */ assertEquals(RESULT, getUninterruptibly(future, 0, SECONDS)); }
static void verifyTimedGetOnPendingFuture(Future<?> future) { try { getUninterruptibly(future, 0, SECONDS); fail(); } catch (TimeoutException expected) { } catch (ExecutionException e) { throw failureWithCause(e, ""); } }
public void testMakeUninterruptible_timedGetNegativeTimeoutAttempted() throws TimeoutException, ExecutionException { SettableFuture<String> future = SettableFuture.create(); future.set(RESULT); /* * The getUninterruptibly should call the timed get method once with a * wait of -1 seconds (and it should succeed, since the result is already * available). */ assertEquals(RESULT, getUninterruptibly(future, -1, SECONDS)); }
@Override public void run() { Future<String> future = currentFuture.get(); while (true) { try { String result = Uninterruptibles.getUninterruptibly(future, 0, TimeUnit.SECONDS); finalResults.add(result); break; } catch (ExecutionException e) { finalResults.add(e.getCause()); break; } catch (CancellationException e) { finalResults.add(CancellationException.class); break; } catch (TimeoutException e) { // loop } } awaitUnchecked(barrier); } };
@Override public void run() { Future<String> future = currentFuture.get(); while (true) { try { String result = Uninterruptibles.getUninterruptibly(future, 0, TimeUnit.SECONDS); finalResults.add(result); break; } catch (ExecutionException e) { finalResults.add(e.getCause()); break; } catch (CancellationException e) { finalResults.add(CancellationException.class); break; } catch (TimeoutException e) { // loop } } awaitUnchecked(barrier); } };
/** * Retrieves the result of a {@code Future} known to be done but uses the {@code get(long, * TimeUnit)} overload in order to test that method. */ static <V> V getDoneFromTimeoutOverload(Future<V> future) throws ExecutionException { checkState(future.isDone(), "Future was expected to be done: %s", future); try { return getUninterruptibly(future, 0, SECONDS); } catch (TimeoutException e) { AssertionFailedError error = new AssertionFailedError(e.getMessage()); error.initCause(e); throw error; } }
/** * Refreshes the value associated with {@code key}, unless another thread is already doing so. * Returns the newly refreshed value associated with {@code key} if it was refreshed inline, or * {@code null} if another thread is performing the refresh or if an error occurs during * refresh. */ @Nullable V refresh(K key, int hash, CacheLoader<? super K, V> loader, boolean checkTime) { final LoadingValueReference<K, V> loadingValueReference = insertLoadingValueReference(key, hash, checkTime); if (loadingValueReference == null) { return null; } ListenableFuture<V> result = loadAsync(key, hash, loadingValueReference, loader); if (result.isDone()) { try { return Uninterruptibles.getUninterruptibly(result); } catch (Throwable t) { // don't let refresh exceptions propagate; error was already logged } } return null; }
public void testMakeUninterruptible_timeoutPreservedThroughInterruption() throws ExecutionException { repeatedlyInterruptTestThread(100, tearDownStack); try { getUninterruptibly(delayedFuture, 500, TimeUnit.MILLISECONDS); fail("expected to time out"); } catch (TimeoutException expected) { } assertTrue(Thread.interrupted()); // clears the interrupt state, too assertFalse(sleeper.completed); assertTrue(getUninterruptibly(delayedFuture)); assertTrue(Thread.interrupted()); // clears the interrupt state, too assertTrue(sleeper.completed); }
return getUninterruptibly(waiter, timeout, unit); } catch (ExecutionException e) { propagateIfInstanceOf(e.getCause(), ExecutionException.class);
/** Waits uninterruptibly for {@code newValue} to be loaded, and then records loading stats. */ V getAndRecordStats( K key, int hash, LoadingValueReference<K, V> loadingValueReference, ListenableFuture<V> newValue) throws ExecutionException { V value = null; try { value = getUninterruptibly(newValue); if (value == null) { throw new InvalidCacheLoadException("CacheLoader returned null for key " + key + "."); } statsCounter.recordLoadSuccess(loadingValueReference.elapsedNanos()); storeLoadedValue(key, hash, loadingValueReference, value); return value; } finally { if (value == null) { statsCounter.recordLoadException(loadingValueReference.elapsedNanos()); removeLoadingValue(key, hash, loadingValueReference); } } }