本文整理匯總了Java中java.util.concurrent.CompletableFuture.cancel方法的典型用法代碼示例。如果您正苦於以下問題:Java CompletableFuture.cancel方法的具體用法?Java CompletableFuture.cancel怎麽用?Java CompletableFuture.cancel使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.CompletableFuture
的用法示例。
在下文中一共展示了CompletableFuture.cancel方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testWaitAsync_WithCancellation_DoesNotClaimSignal
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test
public void testWaitAsync_WithCancellation_DoesNotClaimSignal() {
CompletableFuture<Void> waitFuture = event.waitAsync();
Assert.assertFalse(waitFuture.isDone());
// Cancel the request and ensure that it propagates to the task.
waitFuture.cancel(true);
try {
waitFuture.join();
Assert.fail("Future was expected to be cancelled.");
} catch (CancellationException ex) {
}
// Now set the event and verify that a future waiter gets the signal immediately.
event.set();
waitFuture = event.waitAsync();
Assert.assertTrue(waitFuture.isDone());
Assert.assertFalse(waitFuture.isCompletedExceptionally());
}
示例2: close
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Override
public void close() {
state.set(State.STOPPED);
if (!initialValueFuture.isDone()) {
initialValueFuture.cancel(false);
}
// Cancel any scheduled operations.
final ScheduledFuture<?> currentScheduleFuture = this.currentScheduleFuture;
if (currentScheduleFuture != null && !currentScheduleFuture.isDone()) {
currentScheduleFuture.cancel(false);
}
final CompletableFuture<?> currentWatchFuture = this.currentWatchFuture;
if (currentWatchFuture != null && !currentWatchFuture.isDone()) {
currentWatchFuture.cancel(false);
}
}
示例3: testCancelDoesNotCancelAll
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test
public void testCancelDoesNotCancelAll() {
CompletableFuture<Void> future1 = event.waitAsync();
CompletableFuture<Void> future2 = event.waitAsync();
// Cancel the first future
try {
future1.cancel(true);
future1.join();
Assert.fail("Expected a CancellationException");
} catch (CancellationException ex) {
}
// Make sure the second future was not cancelled
event.set();
future2.join();
}
示例4: testIfCancelled_cancelledWithInterruption
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test public void testIfCancelled_cancelledWithInterruption() {
AtomicReference<CancellationException> cancelled = new AtomicReference<>();
CompletableFuture<String> future = new CompletableFuture<>();
future.cancel(true);
Utils.ifCancelled(future, cancelled::set);
assertThat(cancelled.get()).isInstanceOf(CancellationException.class);
}
示例5: testCancelledConsume
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* consume eventually stops processing published items if cancelled
*/
public void testCancelledConsume() {
AtomicInteger count = new AtomicInteger();
SubmissionPublisher<Integer> p = basicPublisher();
CompletableFuture<Void> f = p.consume(x -> count.getAndIncrement());
f.cancel(true);
int n = 1000000; // arbitrary limit
for (int i = 1; i <= n; ++i)
p.submit(i);
assertTrue(count.get() < n);
}
示例6: testPropagateCancellation_cancellationWithInterruptionPropagated
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test public void testPropagateCancellation_cancellationWithInterruptionPropagated() {
CompletableFuture<String> outer = new CompletableFuture<>();
CompletableFuture<String> inner = new CompletableFuture<>();
assertThat(Utils.propagateCancellation(outer, inner)).isSameAs(outer);
outer.cancel(true);
assertThat(outer.isCancelled()).isTrue();
assertThat(inner.isCancelled()).isTrue();
assertThat(outer.isDone()).isTrue();
assertThat(inner.isDone()).isTrue();
assertThrows(CancellationException.class, inner::get);
}
示例7: testPropagateCancellation_cancellationWithoutInterruptionPropagated
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test public void testPropagateCancellation_cancellationWithoutInterruptionPropagated() {
CompletableFuture<String> outer = new CompletableFuture<>();
CompletableFuture<String> inner = new CompletableFuture<>();
assertThat(Utils.propagateCancellation(outer, inner)).isSameAs(outer);
outer.cancel(false);
assertThat(outer.isCancelled()).isTrue();
assertThat(inner.isCancelled()).isTrue();
assertThat(outer.isDone()).isTrue();
assertThat(inner.isDone()).isTrue();
assertThrows(CancellationException.class, inner::get);
}
示例8: testPropagateCancellation_innerAlreadyCancelled
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test public void testPropagateCancellation_innerAlreadyCancelled() {
CompletableFuture<String> outer = new CompletableFuture<>();
CompletableFuture<String> inner = new CompletableFuture<>();
assertThat(Utils.propagateCancellation(outer, inner)).isSameAs(outer);
inner.cancel(false);
outer.cancel(false);
assertThat(outer.isCancelled()).isTrue();
assertThat(inner.isCancelled()).isTrue();
assertThat(outer.isCompletedExceptionally()).isTrue();
assertThat(inner.isCompletedExceptionally()).isTrue();
assertThat(outer.isDone()).isTrue();
assertThat(inner.isDone()).isTrue();
}
示例9: testPropagateCancellation_innerAlreadyCompleted
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test public void testPropagateCancellation_innerAlreadyCompleted() throws Exception {
CompletableFuture<String> outer = new CompletableFuture<>();
CompletableFuture<String> inner = new CompletableFuture<>();
assertThat(Utils.propagateCancellation(outer, inner)).isSameAs(outer);
inner.complete("inner");
outer.cancel(false);
assertThat(outer.isCancelled()).isTrue();
assertThat(inner.isCancelled()).isFalse();
assertThat(outer.isCompletedExceptionally()).isTrue();
assertThat(inner.isCompletedExceptionally()).isFalse();
assertThat(outer.isDone()).isTrue();
assertThat(inner.isDone()).isTrue();
assertThat(inner.get()).isEqualTo("inner");
}
示例10: testPropagateCancellation_innerAlreadyFailed
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test public void testPropagateCancellation_innerAlreadyFailed() {
CompletableFuture<String> outer = new CompletableFuture<>();
CompletableFuture<String> inner = new CompletableFuture<>();
assertThat(Utils.propagateCancellation(outer, inner)).isSameAs(outer);
IOException exception = new IOException();
inner.completeExceptionally(exception);
outer.cancel(false);
assertThat(outer.isCancelled()).isTrue();
assertThat(inner.isCancelled()).isFalse();
assertThat(outer.isCompletedExceptionally()).isTrue();
assertThat(inner.isCompletedExceptionally()).isTrue();
assertThat(outer.isDone()).isTrue();
assertThat(inner.isDone()).isTrue();
assertCauseOf(ExecutionException.class, inner).isSameAs(exception);
}
示例11: awaitCompletion
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
private void awaitCompletion(final List<CompletableFuture<Void>> deployments) throws InterruptedException, ExecutionException {
try {
CompletableFuture.allOf(deployments.toArray(new CompletableFuture[deployments.size()])).get();
}
finally {
for (final CompletableFuture<Void> future : deployments) {
future.cancel(true);
}
}
}
示例12: serverStopping
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
public void serverStopping() {
serverStopping = true;
final CompletableFuture<?>[] futures =
pendingFutures.toArray(new CompletableFuture[pendingFutures.size()]);
for (CompletableFuture<?> f : futures) {
pendingFutures.remove(f);
f.cancel(false);
}
}
示例13: forwardCancelShouldWork
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Test
public void forwardCancelShouldWork() {
CompletableFuture<String> cf = new CompletableFuture<>();
ListenableFuture<String> lf = MoreFutures.fromCompletableFuture(cf);
cf.cancel(true);
assertThat(lf.isDone()).isTrue();
assertThat(lf.isCancelled()).isTrue();
}
示例14: futureOf
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Override
public CompletableFuture<ClusterTopology> futureOf(Predicate<ClusterTopology> predicate) {
ArgAssert.notNull(predicate, "Predicate");
CompletableFuture<ClusterTopology> future = new CompletableFuture<>();
ClusterEventListener listener = new ClusterEventListener() {
@Override
public void onEvent(ClusterEvent event) {
InitializationContext localCtx = requireContext();
if (future.isDone()) {
localCtx.cluster().removeListener(this);
} else if (predicate.test(event.topology())) {
localCtx.cluster().removeListener(this);
future.complete(event.topology());
} else if (event.type() == ClusterEventType.LEAVE) {
localCtx.cluster().removeListener(this);
future.cancel(false);
}
}
};
guard.lockRead();
try {
if (guard.isInitialized()) {
requireContext().cluster().addListenerAsync(listener);
} else {
deferredListeners.add(new DeferredListener(listener, null));
}
} finally {
guard.unlockRead();
}
return future;
}
示例15: testAwaitAsyncCompletedThenCancelledFunction
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* This test is one part of verifying https://github.com/tunnelvisionlabs/java-threading/issues/11.
*/
@Test
public void testAwaitAsyncCompletedThenCancelledFunction() {
CompletableFuture<Void> cancellationFuture = new CompletableFuture<>();
CompletableFuture<Void> asyncTest = Async.awaitAsync(
Futures.completedNull(),
ignored -> cancellationFuture);
cancellationFuture.cancel(true);
thrown.expect(CancellationException.class);
asyncTest.join();
}