当配置 tomcat 只使用 1 个线程时,当使用 completablefuture 进行异步处理时,它正在使用另一个线程怎么办?

While configuring tomcat to use only 1 thread, when using completablefuture for async processing, it is using another thread how?

我有一个端点使用 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 方法文档说:

Returns a new CompletableFuture that is asynchronously completed by a task running in the ForkJoinPool.commonPool() with the value obtained by calling the given Supplier.

公共池由JVM创建,在ForkJoinPool API doc:

中有描述

A static commonPool() is available and appropriate for most applications. The common pool is used by any ForkJoinTask that is not explicitly submitted to a specified pool. Using the common pool normally reduces resource usage (its threads are slowly reclaimed during periods of non-use, and reinstated upon subsequent use).

由于此池不是由 Tomcat 创建的,因此最大线程限制不适用。