我有一个使用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创建的,因此不适用最大线程限制。