CompletableFuture

ExecutorService cpuBound = Executors.newFixedThreadPool(4);
or
ExecutorService ioBound = Executors.newCachedThreadPool(200);

CompletableFuture.supplyAsync(() -> getOrder(), ioBound)
				.thenApplyAsync(order -> enrich(order), cpuBound)
				.thenApplyAsync(order -> getOrder(order))
				.exectionally(e -> new Fail())
				.thenAccept(order -> sendEmail(order));
				

thenApplyAsync 和 thenApply区别是。 thenApply是保持在同一个线程。thenApplyAsync是可以用另一个线程来执行。
第三行exectionally 可以catch any exception send out from getOrder/enrich/ getOrder
第四行如果没有指定线程池,则用默认ForkJoinPool.commonPool() 来进行。CompletableFuture

  1. FixedThreadPool
  2. CachedThreadPool
  3. ScheduledThreadPool
  4. SinglethreadedExecutor
    CompletableFuture
    submit tasks:
    CompletableFuture

相关文章:

  • 2021-10-06
  • 2021-12-18
  • 2021-12-25
  • 2021-11-25
  • 2021-07-22
  • 2021-07-30
  • 2022-02-05
猜你喜欢
  • 2022-12-23
  • 2021-12-13
  • 2021-10-21
  • 2021-11-04
  • 2021-09-08
相关资源
相似解决方案