當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。