【发布时间】:2019-02-07 22:03:56
【问题描述】:
我对在 CompletableFuture 中定义执行者感到困惑。我不确定如何告诉 CompletableFuture 在该特定执行程序中运行它。提前致谢。
//Suppose I have an executor
ExecutorService myExecutor=Executors.newFixedThreadPool(2);
//If I create a future like this
CompletableFuture.runAsync(() -> {
//Do something
}, myExecutor); // I can put the executor here and say the future to this executor
//But I do not know where to put executor if I create my future in method style like this
private final CompletableFuture<Void> myMethod(String something) {
//Do something
return null;
}
//and use it like this
.thenCompose(this::myMethod); //How can I specify the executor in this case?
【问题讨论】:
-
你是在问如何在执行器中运行
myMethod,就像使用thenComposeAsync而不是thenCompose一样简单,或者你是在问如何控制@内发生的事情987654325@关于返回的未来,这是不可能的?
标签: java asynchronous executorservice completable-future concurrent.futures