看看下面的两个类,哪个是建议的,它们又有什么不同?。
// Approach 1
public class Test extends Controller {
private final DatabaseExecutionContext executionContext;
@Inject
public Test(DatabaseExecutionContext executionContext) {
this.executionContext = executionContext;
}
public CompletableFuture<Result> get() throws ExecutionException,InterruptedException {
return io().thenApply(Results::ok);
}
public CompletableFuture<String> io() throws ExecutionException,InterruptedException {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return "io";
},executionContext);
}
}
//Approach 2
public class Test extends Controller {
private final DatabaseExecutionContext executionContext;
@Inject
public Test(DatabaseExecutionContext executionContext) {
this.executionContext = executionContext;
}
public Result get() throws ExecutionException,InterruptedException {
return Results.ok(io());
}
public String io() throws ExecutionException,executionContext).join();
}
}
游戏对我来说是新手,我想了解如何正确处理阻塞的IO调用。 有人可以解释一下Play框架如何使用Completable Future,以我的观点,它仍然处于阻塞状态并且没有太大意义,并使我的代码处理的CompletableFuture本身就很可怕。