本文整理匯總了Java中java.util.concurrent.CompletableFuture.completeAsync方法的典型用法代碼示例。如果您正苦於以下問題:Java CompletableFuture.completeAsync方法的具體用法?Java CompletableFuture.completeAsync怎麽用?Java CompletableFuture.completeAsync使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類java.util.concurrent.CompletableFuture
的用法示例。
在下文中一共展示了CompletableFuture.completeAsync方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: completeResponse
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
@Override
void completeResponse(Response r) {
HttpResponseImpl.logResponse(r);
pushCF.complete(r); // not strictly required for push API
// start reading the body using the obtained BodyProcessor
CompletableFuture<Void> start = new MinimalFuture<>();
start.thenCompose( v -> readBodyAsync(getPushHandler(), false, getExchange().executor()))
.whenComplete((T body, Throwable t) -> {
if (t != null) {
responseCF.completeExceptionally(t);
} else {
HttpResponseImpl<T> response = new HttpResponseImpl<>(r.request, r, body, getExchange());
responseCF.complete(response);
}
});
start.completeAsync(() -> null, getExchange().executor());
}
示例2: multiResponseAsync
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
CompletableFuture<U> multiResponseAsync() {
CompletableFuture<Void> start = new MinimalFuture<>();
CompletableFuture<HttpResponseImpl<T>> cf = responseAsync0(start);
CompletableFuture<HttpResponse<T>> mainResponse =
cf.thenApply((HttpResponseImpl<T> b) -> {
multiResponseHandler.onResponse(b);
return (HttpResponse<T>)b;
});
pushGroup.setMainResponse(mainResponse);
// set up house-keeping related to multi-response
mainResponse.thenAccept((r) -> {
// All push promises received by now.
pushGroup.noMorePushes(true);
});
CompletableFuture<U> res = multiResponseHandler.completion(pushGroup.groupResult(), pushGroup.pushesCF());
start.completeAsync( () -> null, executor); // trigger execution
return res;
}
示例3: supply
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
public static <U> CompletableFuture<U> supply(ExceptionalSupplier<U> supplier, Executor executor) {
CompletableFuture<U> cf = new MinimalFuture<>();
cf.completeAsync( () -> {
try {
return supplier.get();
} catch (Throwable ex) {
throw new CompletionException(ex);
}
}, executor);
return cf;
}
示例4: testCompleteAsync
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* completeAsync completes with value of given supplier
*/
public void testCompleteAsync() {
for (Integer v1 : new Integer[] { 1, null })
{
CompletableFuture<Integer> f = new CompletableFuture<>();
f.completeAsync(() -> v1);
f.join();
checkCompletedNormally(f, v1);
}}
示例5: testCompleteAsync2
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* completeAsync completes exceptionally if given supplier throws
*/
public void testCompleteAsync2() {
CompletableFuture<Integer> f = new CompletableFuture<>();
CFException ex = new CFException();
f.completeAsync(() -> { throw ex; });
try {
f.join();
shouldThrow();
} catch (CompletionException success) {}
checkCompletedWithWrappedException(f, ex);
}
示例6: testCompleteAsync3
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
/**
* completeAsync with given executor completes with value of given supplier
*/
public void testCompleteAsync3() {
for (Integer v1 : new Integer[] { 1, null })
{
CompletableFuture<Integer> f = new CompletableFuture<>();
ThreadExecutor executor = new ThreadExecutor();
f.completeAsync(() -> v1, executor);
assertSame(v1, f.join());
checkCompletedNormally(f, v1);
assertEquals(1, executor.count.get());
}}
示例7: 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());
}
示例8: responseAsync
import java.util.concurrent.CompletableFuture; //導入方法依賴的package包/類
public CompletableFuture<HttpResponseImpl<T>> responseAsync() {
CompletableFuture<Void> start = new MinimalFuture<>();
CompletableFuture<HttpResponseImpl<T>> cf = responseAsync0(start);
start.completeAsync( () -> null, executor); // trigger execution
return cf;
}