Tech
[程序员] Java 的 CompletableFuture API 日常开发中使用得多么,怎么感觉这个 API 很怪
我是 Java 程序员,但我也知道 JS 里面有个 Promise ,感觉 Promise 的语法比 Java 的 CompletableFuture 这套 API 优雅很多。 Promise 总共也需要知道 then , catch 两种语句,而 CompletableFuture 则各种方法都有十几二十个了。
还有些匪夷所思的行为,比如下面这个:
同步编排(无 Async 后缀)的方法,例如 thenCompose 不会把任务重新提交给线程池排队,而是尽量复用现有的线程。 具体用哪个线程,取决于代码运行时的时机
情况 A (前置任务还没执行完): 当你调用 thenCompose 时,如果前置任务还在跑,那么 thenCompose 里的代码会在前置任务所在的线程中接着运行。
情况 B (前置任务已经执行完了): 当你调用 thenCompose 时,如果前置任务早就跑完了,那么 thenCompose 里的代码会在当前调用代码的主线程( Caller Thread )中立刻运行。
CompletableFuture<Strin...
Read the full discussion on V2EX
This article was aggregated from V2EX. Click to join the conversation.
View on V2EX