问题描述
我有一个使用CompletableFuture进行异步处理的端点,并且已将嵌入式tomcat配置为只有一个线程,如下所示:
server.tomcat.max-threads=1
我的端点如下:
@RequestMapping(path = "/asyncCompletable",method = RequestMethod.GET)
public CompletableFuture<String> getValueAsyncUsingCompletableFuture() {
log.info("Request received");
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(this::processRequest);
log.info("Servlet thread released");
return completableFuture;
}
在浏览器中多次击中端点(例如,同时击中3次)时,控制台日志如下:
19:20:19.234 [http-nio-9191-exec-1] Request received
19:20:19.234 [http-nio-9191-exec-1] Servlet thread released
19:20:19.234 [ForkJoinPool.commonPool-worker-0] Start processing request
19:20:19.839 [http-nio-9191-exec-1] Request received
19:20:19.859 [http-nio-9191-exec-1] Servlet thread released
19:20:19.859 [ForkJoinPool.commonPool-worker-1] Start processing request
19:20:20.595 [http-nio-9191-exec-1] Request received
19:20:20.596 [http-nio-9191-exec-1] Servlet thread released
19:20:20.596 [ForkJoinPool.commonPool-worker-2] Start processing request
19:20:24.235 [ForkJoinPool.commonPool-worker-0] Completed processing request
19:20:24.235 [ForkJoinPool.commonPool-worker-0] Start reversing string
19:20:24.235 [ForkJoinPool.commonPool-worker-0] Completed reversing string
19:20:24.860 [ForkJoinPool.commonPool-worker-1] Completed processing request
19:20:24.860 [ForkJoinPool.commonPool-worker-1] Start reversing string
19:20:24.860 [ForkJoinPool.commonPool-worker-1] Completed reversing string
19:20:25.596 [ForkJoinPool.commonPool-worker-2] Completed processing request
19:20:25.597 [ForkJoinPool.commonPool-worker-2] Start reversing string
如您所见,由于我已配置tomcat在其线程池中只有1个线程,因此对于所有3个请求,它都使用 http-nio-9191-exec-1 ,但是由于我在使用CompletableFuture时,它正在使用其他线程(例如,例如ForkJoinPool.commonPool-worker-2 )来处理异步任务。 它从哪里使用新线程?因为我在tomcat线程池中只有一个线程。
解决方法
supplyAsync方法文档说:
返回一个新的CompletableFuture,它由在ForkJoinPool.commonPool()中运行的任务异步完成,并具有通过调用给定的Supplier所获得的值。
公共池由JVM创建,在ForkJoinPool API doc中进行了描述:
静态commonPool()可用并且适用于大多数应用程序。任何未显式提交到指定池的ForkJoinTask都使用公共池。使用公共池通常会减少资源使用(在不使用期间缓慢回收其线程,并在以后使用时将其恢复。)
由于该池不是由Tomcat创建的,因此最大线程数限制不适用。