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)
| 归档时间: |
|
| 查看次数: |
888 次 |
| 最近记录: |