当前位置: 首页>>代码示例>>Java>>正文


Java CompletableFuture.join方法代码示例

本文整理汇总了Java中java.util.concurrent.CompletableFuture.join方法的典型用法代码示例。如果您正苦于以下问题:Java CompletableFuture.join方法的具体用法?Java CompletableFuture.join怎么用?Java CompletableFuture.join使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.CompletableFuture的用法示例。


在下文中一共展示了CompletableFuture.join方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testValueFactoryReentersValueFactoryAsynchronously

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
private void testValueFactoryReentersValueFactoryAsynchronously(boolean specifyJff) {
	// use our own so we don't get main thread deadlocks, which isn't the point of this test.
	JoinableFutureFactory jtf = specifyJff ? new JoinableFutureContext().getFactory() : null;
	StrongBox<AsyncLazy<Object>> lazy = new StrongBox<>();
	AtomicBoolean executed = new AtomicBoolean(false);
	lazy.value = new AsyncLazy<>(() -> {
		Assert.assertFalse(executed.get());
		executed.set(true);
		return Async.awaitAsync(
			Async.yieldAsync(),
			() -> Async.awaitAsync(
				lazy.value.getValueAsync(),
				() -> CompletableFuture.completedFuture(new Object())));
	}, jtf);

	CompletableFuture<Void> asyncTest = Async.awaitAsync(
		AsyncAssert.assertThrowsAsync(IllegalStateException.class, () -> lazy.value.getValueAsync()),
		() -> {
			// Do it again, to verify that AsyncLazy recorded the failure and will replay it.
			return Async.awaitAsync(AsyncAssert.assertThrowsAsync(IllegalStateException.class, () -> lazy.value.getValueAsync()));
		});

	asyncTest.join();
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:25,代码来源:AsyncLazyTest.java

示例2: testPollAsyncPrecancelled

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void testPollAsyncPrecancelled() {
	CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
	cancellationTokenSource.cancel();
	CompletableFuture<GenericParameterHelper> dequeueTask = queue.pollAsync(cancellationTokenSource.getToken());
	Assert.assertTrue(dequeueTask.isDone());
	CompletableFuture<Void> asyncTest = Async.awaitAsync(
		AsyncAssert.assertCancelsAsync(() -> dequeueTask),
		() -> {
			GenericParameterHelper enqueuedValue = new GenericParameterHelper(1);
			this.queue.add(enqueuedValue);

			Assert.assertEquals(1, this.queue.size());
			return Futures.completedNull();
		});

	asyncTest.join();
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:19,代码来源:AsyncQueueTest.java

示例3: testPollAsyncCancelledBeforeComplete

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void testPollAsyncCancelledBeforeComplete() {
	CompletableFuture<GenericParameterHelper> pollFuture = queue.pollAsync();
	Assert.assertFalse(pollFuture.isDone());

	Assert.assertTrue(pollFuture.cancel(true));

	try {
		thrown.expect(CancellationException.class);
		pollFuture.join();
	} finally {
		GenericParameterHelper addedValue = new GenericParameterHelper(1);
		queue.add(addedValue);

		Assert.assertEquals(1, this.queue.size());
	}
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:18,代码来源:AsyncQueueTest.java

示例4: testIsMainThreadBlockedFalseWhenSyncBlockingOtherThread

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void testIsMainThreadBlockedFalseWhenSyncBlockingOtherThread() {
	CompletableFuture<Void> task = Futures.runAsync(() -> {
		getFactory().run(() -> {
			Assert.assertFalse(getContext().isMainThreadBlocked());
			return Async.awaitAsync(
				Async.yieldAsync(),
				() -> {
					Assert.assertFalse(getContext().isMainThreadBlocked());
					return Futures.completedNull();
				});
		});
	});

	task.join();
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:17,代码来源:JoinableFutureContextTest.java

示例5: testSynchronizationContextNoAppliedToThreadPool_Supplier

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void testSynchronizationContextNoAppliedToThreadPool_Supplier() {
	SynchronizationContext context = new SynchronizationContext() {
		// Sometimes SynchronizationContext.class is used to trigger special behavior, so we force it to extend the
		// base class.
	};

	Thread currentThread = Thread.currentThread();
	SynchronizationContext.setSynchronizationContext(context);
	CompletableFuture<Void> asyncTest = Futures.runAsync(() -> {
		Assert.assertNotSame(currentThread, Thread.currentThread());
		Assert.assertNotSame(context, SynchronizationContext.getCurrent());
		return Futures.completedNull();
	});

	asyncTest.join();
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:18,代码来源:SynchronizationContextTest.java

示例6: testMinimalCompletionStage_join_by_hand

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
 * Joining a minimal stage "by hand" works
 */
public void testMinimalCompletionStage_join_by_hand() {
    for (boolean createIncomplete : new boolean[] { true, false })
    for (Integer v1 : new Integer[] { 1, null })
{
    CompletableFuture<Integer> f = new CompletableFuture<>();
    CompletionStage<Integer> minimal = f.minimalCompletionStage();
    CompletableFuture<Integer> g = new CompletableFuture<>();
    if (!createIncomplete) assertTrue(f.complete(v1));
    minimal.thenAccept(x -> g.complete(x));
    if (createIncomplete) assertTrue(f.complete(v1));
    g.join();
    checkCompletedNormally(g, v1);
    checkCompletedNormally(f, v1);
    assertEquals(v1, join(minimal));
}}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:CompletableFutureTest.java

示例7: verifyDoesNotInlineContinuations

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
 * Verifies that continuations scheduled on a future will not be executed inline with the specified completing
 * action.
 *
 * @param antecedent The future to test.
 * @param completingAction The action that results in the synchronous completion of the future.
 */
protected static void verifyDoesNotInlineContinuations(@NotNull CompletableFuture<?> antecedent, @NotNull Runnable completingAction) {
	Requires.notNull(antecedent, "antecedent");
	Requires.notNull(completingAction, "completingAction");

	CompletableFuture<Void> completingActionFinished = new CompletableFuture<>();
	CompletableFuture<Void> continuation = antecedent.handle((result, exception) -> {
		try {
			return completingActionFinished.get(ASYNC_DELAY.toMillis(), TimeUnit.MILLISECONDS);
		} catch (InterruptedException | ExecutionException | TimeoutException ex) {
			throw new CompletionException(ex);
		}
	});

	completingAction.run();
	completingActionFinished.complete(null);

	// Rethrow the exception if it turned out it deadlocked.
	continuation.join();
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:27,代码来源:TestBase.java

示例8: testSetGetWithDelay

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void testSetGetWithDelay() {
	GenericParameterHelper value = new GenericParameterHelper();
	asyncLocal.setValue(value);
	CompletableFuture<Void> asyncTest = Async.delayAsync(Duration.ofMillis(10)).thenRun(
		() -> {
			Assert.assertSame(value, asyncLocal.getValue());
			asyncLocal.setValue(null);
			Assert.assertNull(asyncLocal.getValue());
		});

	asyncTest.join();
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:14,代码来源:AsyncLocalTest.java

示例9: testSetGetWithYield

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void testSetGetWithYield() {
	GenericParameterHelper value = new GenericParameterHelper();
	asyncLocal.setValue(value);
	CompletableFuture<Void> asyncTest = Async.awaitAsync(
		Async.yieldAsync(),
		() -> {
			Assert.assertSame(value, asyncLocal.getValue());
			asyncLocal.setValue(null);
			Assert.assertNull(asyncLocal.getValue());
			return Futures.completedNull();
		});

	asyncTest.join();
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:16,代码来源:AsyncLocalTest.java

示例10: testAwaitable

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void testAwaitable() {
	CompletableFuture<Void> task = Futures.runAsync(() -> {
		return Async.awaitAsync(this.event);
	});

	this.event.set();
	task.join();
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:10,代码来源:AsyncManualResetEventTest.java

示例11: testGetValueAsyncWithCancellationTokenPreCanceled

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void testGetValueAsyncWithCancellationTokenPreCanceled() {
	CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
	cancellationTokenSource.cancel();

	AsyncLazy<GenericParameterHelper> lazy = new AsyncLazy<>(() -> CompletableFuture.completedFuture(new GenericParameterHelper(5)));
	CompletableFuture<Void> asyncTest = Async.awaitAsync(
		AsyncAssert.assertCancelsAsync(() -> lazy.getValueAsync(cancellationTokenSource.getToken())),
		() -> {
			Assert.assertFalse("Value factory should not have been invoked for a pre-canceled token.", lazy.isValueCreated());
			return Futures.completedNull();
		});

	asyncTest.join();
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:16,代码来源:AsyncLazyTest.java

示例12: testCompleteAsync4

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
 * completeAsync with given executor completes exceptionally if
 * given supplier throws
 */
public void testCompleteAsync4() {
    CompletableFuture<Integer> f = new CompletableFuture<>();
    CFException ex = new CFException();
    ThreadExecutor executor = new ThreadExecutor();
    f.completeAsync(() -> { throw ex; }, executor);
    try {
        f.join();
        shouldThrow();
    } catch (CompletionException success) {}
    checkCompletedWithWrappedException(f, ex);
    assertEquals(1, executor.count.get());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:CompletableFutureTest.java

示例13: testAwaitAsyncCompletedThenCancelledSupplier

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
 * This test is one part of verifying https://github.com/tunnelvisionlabs/java-threading/issues/11.
 */
@Test
public void testAwaitAsyncCompletedThenCancelledSupplier() {
	CompletableFuture<Void> cancellationFuture = new CompletableFuture<>();
	CompletableFuture<Void> asyncTest = Async.awaitAsync(
		Futures.completedNull(),
		() -> cancellationFuture);

	cancellationFuture.cancel(true);

	thrown.expect(CancellationException.class);
	asyncTest.join();
}
 
开发者ID:tunnelvisionlabs,项目名称:java-threading,代码行数:16,代码来源:AsyncTest.java

示例14: testConsume

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
/**
 * consume returns a CompletableFuture that is done when
 * publisher completes
 */
public void testConsume() {
    AtomicInteger sum = new AtomicInteger();
    SubmissionPublisher<Integer> p = basicPublisher();
    CompletableFuture<Void> f =
        p.consume((Integer x) -> sum.getAndAdd(x.intValue()));
    int n = 20;
    for (int i = 1; i <= n; ++i)
        p.submit(i);
    p.close();
    f.join();
    assertEquals((n * (n + 1)) / 2, sum.get());
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:SubmissionPublisherTest.java

示例15: itExecutesReturnedExceptionalFuturesOnTheProvidedExecutor

import java.util.concurrent.CompletableFuture; //导入方法依赖的package包/类
@Test
public void itExecutesReturnedExceptionalFuturesOnTheProvidedExecutor() {
  ExecutorService executorService = Executors.newSingleThreadExecutor(new ThreadFactoryBuilder().setNameFormat("SmtpSessionTestExecutor").build());
  SmtpSession session = new SmtpSession(channel, responseHandler, CONFIG, executorService, SSL_ENGINE_SUPPLIER);

  CompletableFuture<SmtpClientResponse> future = session.send(SMTP_REQUEST);
  CompletableFuture<Boolean> assertionFuture = future.handle((r, e) -> {
    assertThat(Thread.currentThread().getName()).contains("SmtpSessionTestExecutor");
    return true;
  });

  responseFuture.completeExceptionally(new RuntimeException());
  assertionFuture.join();
}
 
开发者ID:HubSpot,项目名称:NioSmtpClient,代码行数:15,代码来源:SmtpSessionTest.java


注:本文中的java.util.concurrent.CompletableFuture.join方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。