本文整理汇总了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;
}