当 Spring @Async 线程从未完成时会发生什么?

Jas*_*son 5 multithreading java.util.concurrent spring-boot spring-async java-11

如果你有一个 @Async 方法,它返回一个CompletableFuture.... 并且 future永远不会完成,spring 是否会泄漏线程?是的,我知道任何等待结果的人都可能超时并假设后期阶段异常完成......但这不会停止线程。即使你调用cancel,它也不会影响正在运行的线程:

来自文档:

@param mayInterruptIfRunning 该值在此实现中无效,因为中断不用于控制处理。

如果我使用 Future 而不是 CompletableFuture,cancel将会中断线程。不幸的是,Future 上没有相当于 CompletableFuture 上的“allOf”来等待所有任务,如下所示:

// wait for all the futures to finish, regardless of results
CompletableFuture.allOf(futures.toArray(CompletableFuture[]::new))
   // if exceptions happened in any future, swallow them
   // I don't care because I'm going to process each future in my list anyway
   // we just wanted to wait for all the futures to finish
   .exceptionally(ex -> null); 
Run Code Online (Sandbox Code Playgroud)
  1. 我们应该如何取消我们放弃的线程?
  2. 如果我没有取消它(以某种方式),我的池会永远处于线程状态吗???