当前位置: 首页>>代码示例>>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;未经允许,请勿转载。