java在配置tomcat仅使用1个线程时,在使用completablefuture进行异步处理时,它是如何使用另一个线程的?
我有一个端点使用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线程池中只有一个线程可用
# 1 楼答案
{a1}方法文档说明:
公共池由JVM创建,如ForkJoinPool API doc中所述:
由于此池不是由Tomcat创建的,因此最大线程限制不适用