本文整理了Java中com.google.common.util.concurrent.SettableFuture
类的一些代码示例,展示了SettableFuture
类的具体用法。这些代码示例主要来源于Github
/Stackoverflow
/Maven
等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SettableFuture
类的具体详情如下:
包路径:com.google.common.util.concurrent.SettableFuture
类名称:SettableFuture
[英]A ListenableFuture whose result can be set by a #set(Object), #setException(Throwable) or #setFuture(ListenableFuture) call. It can also, like any other Future, be #cancel.
SettableFuture is the recommended ListenableFuture implementation when your task cannot be implemented with ListeningExecutorService, the various Futuresutility methods, or ListenableFutureTask. Those APIs have less opportunity for developer error. If your needs are more complex than SettableFuture supports, use AbstractFuture, which offers an extensible version of the API.
[中]一种ListenableFuture,其结果可以通过#set(对象)、#setException(可丢弃)或#setFuture(ListenableFuture)调用来设置。它也可以像任何其他未来一样被#取消。
当无法使用ListeningExecutorService、各种FutureSuitability方法或ListenableFutureTask实现任务时,建议使用SettableFuture实现ListenableFuture。这些API不太可能出现开发人员错误。如果您的需求比SettableFuture支持更复杂,请使用AbstractFuture,它提供了API的可扩展版本。
代码示例来源:origin: google/guava
public void testSetValue_simpleThreaded() throws Exception {
SettableFuture<Integer> future = SettableFuture.create();
assertTrue(future.set(42));
// Later attempts to set the future should return false.
assertFalse(future.set(23));
assertFalse(future.setException(new Exception("bar")));
assertFalse(future.setFuture(SettableFuture.<Integer>create()));
// Check that the future has been set properly.
assertTrue(future.isDone());
assertFalse(future.isCancelled());
assertEquals(42, (int) future.get());
}
代码示例来源:origin: prestodb/presto
public synchronized ListenableFuture<?> getNotFullFuture()
{
// if we are full and the current not full future is already complete, create a new one
if (bufferedBytes.get() > maxBufferedBytes && notFullFuture.isDone()) {
notFullFuture = SettableFuture.create();
}
return notFullFuture;
}
代码示例来源:origin: google/guava
public void testSetFuture_stackOverflow() {
SettableFuture<String> orig = SettableFuture.create();
SettableFuture<String> prev = orig;
for (int i = 0; i < 100000; i++) {
SettableFuture<String> curr = SettableFuture.create();
prev.setFuture(curr);
prev = curr;
}
// prev represents the 'innermost' future
prev.set("done");
assertTrue(orig.isDone());
}
代码示例来源:origin: google/guava
public void testCancel_innerCancelsAsync() throws Exception {
SettableFuture<Object> async = SettableFuture.create();
SettableFuture<Object> inner = SettableFuture.create();
async.setFuture(inner);
inner.cancel(true);
assertTrue(async.isCancelled());
try {
async.get();
fail("Expected CancellationException");
} catch (CancellationException expected) {
/* expected */
}
}
代码示例来源:origin: google/guava
public void testSuccessfulAsList_totalFailure() throws Exception {
SingleCallListener listener = new SingleCallListener();
SettableFuture<String> future1 = SettableFuture.create();
SettableFuture<String> future2 = SettableFuture.create();
@SuppressWarnings("unchecked") // array is never modified
ListenableFuture<List<String>> compound = successfulAsList(future1, future2);
compound.addListener(listener, directExecutor());
assertFalse(compound.isDone());
future1.setException(new Throwable("failed1"));
assertFalse(compound.isDone());
listener.expectCall();
future2.setException(new Throwable("failed2"));
assertTrue(listener.wasCalled());
List<String> results = getDone(compound);
assertThat(results).containsExactly(null, null).inOrder();
}
代码示例来源:origin: google/guava
/**
* Test the case where the futures are fulfilled prior to constructing the ListFuture. There was a
* bug where the loop that connects a Listener to each of the futures would die on the last
* loop-check as done() on ListFuture nulled out the variable being looped over (the list of
* futures).
*/
public void testAllAsList_doneFutures() throws Exception {
// Create input and output
SettableFuture<String> future1 = SettableFuture.create();
SettableFuture<String> future2 = SettableFuture.create();
SettableFuture<String> future3 = SettableFuture.create();
// Satisfy each input prior to creating compound and check the output
future1.set(DATA1);
future2.set(DATA2);
future3.set(DATA3);
@SuppressWarnings("unchecked") // array is never modified
ListenableFuture<List<String>> compound = allAsList(future1, future2, future3);
// Attach a listener
SingleCallListener listener = new SingleCallListener();
listener.expectCall();
compound.addListener(listener, directExecutor());
assertTrue(listener.wasCalled());
List<String> results = getDone(compound);
assertThat(results).containsExactly(DATA1, DATA2, DATA3).inOrder();
}
代码示例来源:origin: google/guava
public void testTransformAsync_cancelPropagatesToInput() throws Exception {
SettableFuture<Foo> input = SettableFuture.create();
AsyncFunction<Foo, Bar> function =
new AsyncFunction<Foo, Bar>() {
@Override
public ListenableFuture<Bar> apply(Foo unused) {
throw new AssertionFailedError("Unexpeted call to apply.");
}
};
assertTrue(transformAsync(input, function, directExecutor()).cancel(false));
assertTrue(input.isCancelled());
assertFalse(input.wasInterrupted());
}
代码示例来源:origin: google/guava
@GwtIncompatible // Mockito
public void testOnSuccessThrowsRuntimeException() throws Exception {
RuntimeException exception = new RuntimeException();
String result = "result";
SettableFuture<String> future = SettableFuture.create();
@SuppressWarnings("unchecked") // Safe for a mock
FutureCallback<String> callback = Mockito.mock(FutureCallback.class);
addCallback(future, callback, directExecutor());
Mockito.doThrow(exception).when(callback).onSuccess(result);
future.set(result);
assertEquals(result, future.get());
Mockito.verify(callback).onSuccess(result);
Mockito.verifyNoMoreInteractions(callback);
}
代码示例来源:origin: google/guava
public void testCallableStartsAfterFirstFutureCompletes() {
@SuppressWarnings({"unused", "nullness"})
Future<?> possiblyIgnoredError = serializer.submitAsync(firstCallable, directExecutor());
TestCallable secondCallable = new TestCallable(Futures.<Void>immediateFuture(null));
@SuppressWarnings({"unused", "nullness"})
Future<?> possiblyIgnoredError1 = serializer.submitAsync(secondCallable, directExecutor());
assertThat(firstCallable.called).isTrue();
assertThat(secondCallable.called).isFalse();
firstFuture.set(null);
assertThat(secondCallable.called).isTrue();
}
代码示例来源:origin: google/guava
@GwtIncompatible // threads
final CountDownLatch inFunction = new CountDownLatch(1);
final CountDownLatch functionDone = new CountDownLatch(1);
final SettableFuture<Integer> resultFuture = SettableFuture.create();
AsyncFunction<String, Integer> function =
new AsyncFunction<String, Integer>() {
SettableFuture<String> inputFuture = SettableFuture.create();
ListenableFuture<Integer> future =
transformAsync(inputFuture, function, newSingleThreadExecutor());
inputFuture.set("value");
inFunction.await();
future.cancel(false);
functionDone.countDown();
try {
future.get();
fail();
} catch (CancellationException expected) {
resultFuture.get();
fail();
} catch (CancellationException expected) {
代码示例来源:origin: google/guava
@GwtIncompatible // get() timeout
public void testTransformAsync_asyncFunction_timeout()
throws InterruptedException, ExecutionException {
AsyncFunction<String, Integer> function = constantAsyncFunction(immediateFuture(1));
ListenableFuture<Integer> future =
transformAsync(SettableFuture.<String>create(), function, directExecutor());
try {
future.get(1, MILLISECONDS);
fail();
} catch (TimeoutException expected) {
}
}
代码示例来源:origin: google/guava
@GwtIncompatible // threads
SettableFuture<String> stringFuture = SettableFuture.create();
SettableFuture<Boolean> booleanFuture = SettableFuture.create();
final CountDownLatch inFunction = new CountDownLatch(1);
final CountDownLatch shouldCompleteFunction = new CountDownLatch(1);
final SettableFuture<String> resultFuture = SettableFuture.create();
AsyncCallable<String> combiner =
new AsyncCallable<String>() {
whenAllComplete(stringFuture, booleanFuture).callAsync(combiner, newSingleThreadExecutor());
stringFuture.set("value");
booleanFuture.set(true);
inFunction.await();
futureResult.cancel(false);
shouldCompleteFunction.countDown();
try {
futureResult.get();
fail();
} catch (CancellationException expected) {
resultFuture.get();
fail();
} catch (CancellationException expected) {
代码示例来源:origin: google/guava
@GwtIncompatible // threads
final CountDownLatch inFunction = new CountDownLatch(1);
final CountDownLatch callableDone = new CountDownLatch(1);
final SettableFuture<Integer> resultFuture = SettableFuture.create();
AsyncCallable<Integer> callable =
new AsyncCallable<Integer>() {
SettableFuture<String> inputFuture = SettableFuture.create();
ListenableFuture<Integer> future = submitAsync(callable, newSingleThreadExecutor());
inputFuture.set("value");
inFunction.await();
future.cancel(false);
callableDone.countDown();
try {
future.get();
fail();
} catch (CancellationException expected) {
resultFuture.get();
fail();
} catch (CancellationException expected) {
代码示例来源:origin: google/guava
public void testAllAsList_resultCancelled() throws Exception {
SettableFuture<String> future1 = SettableFuture.create();
SettableFuture<String> future2 = SettableFuture.create();
@SuppressWarnings("unchecked") // array is never modified
ListenableFuture<List<String>> compound = allAsList(future1, future2);
future2.set(DATA2);
assertFalse(compound.isDone());
assertTrue(compound.cancel(false));
assertTrue(compound.isCancelled());
assertTrue(future1.isCancelled());
assertFalse(future1.wasInterrupted());
}
代码示例来源:origin: google/guava
public void testSameThreadSuccess() {
SettableFuture<String> f = SettableFuture.create();
MockCallback callback = new MockCallback("foo");
addCallback(f, callback, directExecutor());
f.set("foo");
}
代码示例来源:origin: google/guava
public void testAllAsList_failure() throws Exception {
SingleCallListener listener = new SingleCallListener();
SettableFuture<String> future1 = SettableFuture.create();
SettableFuture<String> future2 = SettableFuture.create();
@SuppressWarnings("unchecked") // array is never modified
ListenableFuture<List<String>> compound = allAsList(future1, future2);
compound.addListener(listener, directExecutor());
listener.expectCall();
Throwable exception = new Throwable("failed1");
future1.setException(exception);
assertTrue(compound.isDone());
assertTrue(listener.wasCalled());
assertFalse(future2.isDone());
try {
getDone(compound);
fail();
} catch (ExecutionException expected) {
assertSame(exception, expected.getCause());
}
}
代码示例来源:origin: google/guava
public void testSuccessfulAsList_resultCancelled() throws Exception {
SettableFuture<String> future1 = SettableFuture.create();
SettableFuture<String> future2 = SettableFuture.create();
@SuppressWarnings("unchecked") // array is never modified
ListenableFuture<List<String>> compound = successfulAsList(future1, future2);
future2.set(DATA2);
assertFalse(compound.isDone());
assertTrue(compound.cancel(false));
assertTrue(compound.isCancelled());
assertTrue(future1.isCancelled());
assertFalse(future1.wasInterrupted());
}
代码示例来源:origin: google/guava
public void testSameThreadExecutionException() {
SettableFuture<String> f = SettableFuture.create();
Exception e = new IllegalArgumentException("foo not found");
MockCallback callback = new MockCallback(e);
addCallback(f, callback, directExecutor());
f.setException(e);
}
代码示例来源:origin: google/guava
public void testTransform_ErrorAfterCancellation() throws Exception {
class Transformer implements Function<Object, Object> {
ListenableFuture<Object> output;
@Override
public Object apply(Object input) {
output.cancel(false);
throw new MyError();
}
}
Transformer transformer = new Transformer();
SettableFuture<Object> input = SettableFuture.create();
ListenableFuture<Object> output = transform(input, transformer, directExecutor());
transformer.output = output;
input.set("foo");
assertTrue(output.isCancelled());
}
代码示例来源:origin: google/guava
public void testCatchingAsync_rejectionPropagatesToOutput() throws Exception {
SettableFuture<String> input = SettableFuture.create();
ListenableFuture<String> transformed =
catchingAsync(
input,
Throwable.class,
constantAsyncFunction(immediateFuture("foo")),
REJECTING_EXECUTOR);
input.setException(new Exception());
try {
getDone(transformed);
fail();
} catch (ExecutionException expected) {
assertThat(expected.getCause()).isInstanceOf(RejectedExecutionException.class);
}
}
内容来源于网络,如有侵权,请联系作者删除!