當前位置: 首頁>>代碼示例>>Java>>正文


Java CompletableFuture.cancel方法代碼示例

本文整理匯總了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());
}
 
開發者ID:tunnelvisionlabs,項目名稱:java-threading,代碼行數:20,代碼來源:AsyncAutoResetEventTest.java

示例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);
    }
}
 
開發者ID:line,項目名稱:centraldogma,代碼行數:18,代碼來源:AbstractWatcher.java

示例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();
}
 
開發者ID:tunnelvisionlabs,項目名稱:java-threading,代碼行數:18,代碼來源:AsyncManualResetEventTest.java

示例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);
}
 
開發者ID:google,項目名稱:mug,代碼行數:8,代碼來源:UtilsTest.java

示例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);
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:SubmissionPublisherTest.java

示例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);
}
 
開發者ID:google,項目名稱:mug,代碼行數:12,代碼來源:UtilsTest.java

示例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);
}
 
開發者ID:google,項目名稱:mug,代碼行數:12,代碼來源:UtilsTest.java

示例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();
}
 
開發者ID:google,項目名稱:mug,代碼行數:14,代碼來源:UtilsTest.java

示例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");
}
 
開發者ID:google,項目名稱:mug,代碼行數:15,代碼來源:UtilsTest.java

示例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);
}
 
開發者ID:google,項目名稱:mug,代碼行數:16,代碼來源:UtilsTest.java

示例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);
            }
        }
    }
 
開發者ID:stacs-srg,項目名稱:shabdiz,代碼行數:12,代碼來源:ApplicationNetwork.java

示例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);
    }
}
 
開發者ID:line,項目名稱:centraldogma,代碼行數:10,代碼來源:WatchService.java

示例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();
}
 
開發者ID:salesforce,項目名稱:grpc-java-contrib,代碼行數:11,代碼來源:FromCompletableFutureTest.java

示例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;
}
 
開發者ID:hekate-io,項目名稱:hekate,代碼行數:40,代碼來源:DefaultClusterService.java

示例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();
}
 
開發者ID:tunnelvisionlabs,項目名稱:java-threading,代碼行數:16,代碼來源:AsyncTest.java


注:本文中的java.util.concurrent.CompletableFuture.cancel方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。