我有这种方法:
default <U> CompletableFuture<U> submit(Supplier<U> supplier) {
return CompletableFuture.supplyAsync(supplier, .....getThreadPool());
}
Run Code Online (Sandbox Code Playgroud)
它由不同的类广泛使用,但是当前如果计算失败,则没有默认日志记录。我的第一种方法是:
.exceptionally(throwable ->
.....handleThrowable(throwable, runnable);
)
Run Code Online (Sandbox Code Playgroud)
但是此方法用于恢复,因为我必须返回一些东西。如果我只想登录该怎么办?
我正在调用一个返回CompletableFuture的服务.
输出结构如下.
Class Output {
public String name;
public Integer age;
}
Run Code Online (Sandbox Code Playgroud)
我想打电话给服务,并希望继续我的执行,直到名字出现.
就像是,
CompletableFuture<Output> futureOutput = makeServiceCall(input);
String name = futureOutput.get().name;
processName(name); // which will do some costly operations and make use of the name at the end.
Run Code Online (Sandbox Code Playgroud)
在上面的方法中,我需要等到我futureOutput准备好了,即使我以后只需要它.
我寻找类似下面的方法.
CompletableFuture<Output> futureOutput = makeServiceCall(input);
CompletableFuture<String> futureName = futureOutput.get().name; // This is wrong, but can we create a reference in a similar way?
processName(futureName); // which will do some costly operations and make use of the name at …Run Code Online (Sandbox Code Playgroud) 我有一个可完成的未来(future1),它创造了10个可完成的期货(futureN).有没有办法在所有futureN完成后将future1设置为完成?
我已经阅读了很多java8可完成的未来教程,其中大部分基本相同.所有谈论一些基本方法"thenAccept"/"thenApply"/ thenCombine"构建管道流程.
但是当遇到真正的工作问题时,我觉得很难从不同的服务部门组织不同的可完成的未来.例如:
interface Cache{
CompletableFuture<Bean> getAsync(long id);
CompletableFuture<Boolean> saveAsync(Bean bean);
}
interface DB{
Completable<Bean> getAsync(long id)
}
Run Code Online (Sandbox Code Playgroud)
服务逻辑非常简单,从Cache获取数据,如果存在则返回我们的客户端,如果不存在,则从DB获取,如果存在则将其保存回Cache,并将其返回给我们的客户端,如果DB中既不存在,则返回"错误"给客户.
使用同步API,它将非常直接.但是当使用asyncnorized API时,有"很多管道",manny条件中断.我无法弄清楚如何使用CompletableFuture API实现它.
我发现,如果没有完成,它CompletableFuture::join似乎是不可中断的:
// CompletableFuture::join implementation from JDK 8 sources
public T join() {
Object r;
return reportJoin((r = result) == null ? waitingGet(false) : r);
}
Run Code Online (Sandbox Code Playgroud)
在上面的实现中,waitingGet(false)将忽略工作的中断标志Thread并继续等待.我想知道我怎么能打断Thread我打电话的地方CompletableFuture::join.
我写了以下代码:
System.out.println("Main thread:" + Thread.currentThread().getId());
CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
try {
System.out.println("Before sleep thread:" + Thread.currentThread().getId(), + " isDaemon:" + Thread.currentThread().isDaemon());
Thread.sleep(100);
System.out.println("After sleep");
} catch (InterruptedException e) {
e.printStackTrace();
}
});
future.whenComplete((r, e) -> System.out.println("whenCompleted thread:" + Thread.currentThread().getId()));
Run Code Online (Sandbox Code Playgroud)
这一个打印:
Main thread:1
Before sleep thread:11 isDaemon:true
Run Code Online (Sandbox Code Playgroud)
并完成.
我该如何改变这种行为?
PS我在runAsyncjava doc 中看不到任何相关内容
我正在寻找运行两个方法a(),b()它们不接受任何参数并且不异步返回任何内容(即void方法),以便它们以任何顺序并发返回。
但是,第三个方法c()应仅在上述其他方法之一完成后运行。
我相信我应该创建两个CompletableFuture对象(cf1 a()和cf2 b()),然后用于CompletableFuture.anyOf(cf1, cf2).join()阻塞的代码c()。
但是,我不确定如何创建cf1和cf2 CompletableFuture对象。我理解这些方法a(),b()并且本质上类似于Runnable的run方法,但是我不想更改它们的实现方式。为了为这两个方法创建此CompletableFuture对象,应调用哪个CompletableFuture方法?
预先非常感谢您的帮助!
我试图运行以下类,使其终止而不执行CompletableFuture。
public class ThenApplyExample {
public static void main(String[] args) throws Exception {
//ExecutorService es = Executors.newCachedThreadPool();
CompletableFuture<Student> studentCompletableFuture = CompletableFuture.supplyAsync(() -> {
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
return 3;
})// If I put executorservice created n commented above, programme work as expected.
.thenApply(i -> {
for (int j = 0; j <= i; j++) {
System.out.println("Inside first then apply");
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("First then apply is …Run Code Online (Sandbox Code Playgroud) java multithreading java.util.concurrent concurrent.futures completable-future
我正在使用CompletableFuture,并对异常处理有疑问.
我有这样的代码,如果任何validate()或process()方法抛出异常,那么它由ExceptionHandler处理.但是,当我像这样使用CompletableFuture时,抛出的异常包含在CompletionException中.我可以知道如何确保在那里调用我的ExceptionHandler而不是获得CompletionException?
CompletableFuture<Response> response = CompletableFuture
.supplyAsync(() -> {
validationService.validate(request);
return myService.process(request, headers);
});
Run Code Online (Sandbox Code Playgroud) For the following program I am trying to figure out why using 2 different streams parallelizes the task and using the same stream and calling join/get on the Completable future makes them take longer time equivalent to as if they were sequentially processed).
public class HelloConcurrency {
private static Integer sleepTask(int number) {
System.out.println(String.format("Task with sleep time %d", number));
try {
TimeUnit.SECONDS.sleep(number);
} catch (InterruptedException e) {
e.printStackTrace();
return -1;
}
return number;
}
public static void main(String[] args) { …Run Code Online (Sandbox Code Playgroud)