java - 返回一个 CompletableFuture 而不暴露 executor 线程
问题描述
我在库中公开了一个方法,该方法返回一个 CompletableFuture。该方法的计算发生在单线程 Executor 上,这是我的瓶颈,因此我不希望任何后续工作发生在同一个线程上。
如果我使用返回“supplyAsync”结果的简单方法,我会将我宝贵的线程暴露给调用者,调用者可能会添加同步操作(例如通过 thenAccept),这可能会在该线程上占用一些 CPU 时间。
复制如下:
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class CfPlayground {
private ExecutorService preciousExecService = Executors.newFixedThreadPool(1);
CfPlayground() {}
private static void log(String msg) {
System.out.println("[" + Thread.currentThread().getName() + "] " + msg);
}
CompletableFuture<String> asyncOp(String param) {
return CompletableFuture.supplyAsync(() -> {
log("In asyncOp");
return "Hello " + param;
}, preciousExecService);
}
void syncOp(String salutation) {
log("In syncOp: " + salutation);
}
void run() {
log("run");
asyncOp("world").thenAccept(this::syncOp);
}
public static void main(String[] args) throws InterruptedException {
CfPlayground compFuture = new CfPlayground();
compFuture.run();
Thread.sleep(500);
compFuture.preciousExecService.shutdown();
}
}
这确实打印:
[main] run
[pool-1-thread-1] In asyncOp
[pool-1-thread-1] In syncOp: Hello world
我发现的一个解决方案是引入另一个 Executor,并在返回 CompletableFuture 之前添加一个 no-op thenApplyAsync 与该 executor
CompletableFuture<String> asyncOp(String param) {
return CompletableFuture.supplyAsync(() -> {
log("In asyncOp");
return "Hello " + param;
}, preciousExecService).thenApplyAsync(s -> s, secondExecService);
}
这行得通,但感觉不是很优雅——有没有更好的方法来做到这一点?
解决方案
没有功能可以将您的完成与相关操作的执行分开。当链接依赖动作的线程已经完成注册并且您的执行者线程完成未来时,如果没有给出其他执行者,哪个其他线程应该执行依赖动作?
您将另一个动作与不同的执行者联系起来的方法似乎是您能得到的最好的方法。但是,重要的是要注意,在异常完成的情况下,异常会在不评估传递给的函数的情况下传播thenApply
。whenComplete
如果调用者链接了类似、handle
或的动作,这种异常传播可能再次导致线程暴露exceptionally
。
另一方面,您不需要指定辅助执行器,因为您可以使用不带 executor 参数的async方法来获取默认(通用 Fork/Join)池。
因此,到目前为止,链接.whenCompleteAsync((x,y) -> {})
是解决您的问题的最佳方法。
推荐阅读
- python - 为什么在 Python 程序中计算颜色数量时不出现点状图?
- apache-kafka - 卡夫卡独立消费者
- python - Django - 模型混合没有按预期工作
- gradle - Gradle 从包含的 jar 的清单中提取依赖项
- laravel - 如何从通知中获取数据?[错误检索数据] laravel 5.8
- metadata - 元数据 ISO19139-3 验证错误 Geonetwork 3.4.3
- reactjs - 如何在 Excel 行中设置 JSON 数据?
- java - 当相同的 printf 方法在控制台上正确打印出来时,如何解决 Eclipse 调试器窗口上的 java printf 问题(“找不到源。”)?
- angular - 使用 Nativescript Angular 的浮动操作按钮
- python - 违反 UNIQUE KEY 约束 - Pandas to_sql Python SQLAlchemy