I have a question about thread management for the tomcat server and application. Here is a sample code:
@RequestMapping(path = "/asyncCompletable", method = RequestMethod.GET)
public CompletableFuture<String> getValueAsyncUsingCompletableFuture() {
logger.info("Request received");
CompletableFuture<String> completableFuture
= CompletableFuture.supplyAsync(this::processRequest);
logger.info("Servlet thread released");
return completableFuture;
}
private String processRequest() {
Long delayTime = 10000L;
logger.info("Start processing request");
try {
Thread.sleep(delayTime);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
logger.info("Completed processing request");
return "Processing done after " + delayTime;
}
Here, This is a simple API using spring boot. What I have done here?
- It is simple endpoints that is a simple GET method and returning a string as a response.
- In method
getValueAsyncUsingCompletableFutureI am callingprocessRequestonCompletableFuture.supplyAsync(this::processRequest). So obviously it will run under separate thread. - In method
processRequestwhere only have aThread.sleepfor 10s. - Both methods have an entry and exit log.
- Also the API
getValueAsyncUsingCompletableFutureis returning acompletableFuture
Now after calling the Endpoints from the browser its output is as expected. It waits 10 seconds and provides a response. The output log is given below.

Here,
- The first and second line of log is showing under
nio-8080-exec-8thread. It also showing request received and servlet thread released. - In the last 2 lines of log for method
processRequest. Here it is showing forCompletableFutureexecution part. It is handling underonPool-worker-2thread.
Now, Here is my question:
- During the execution of
processRequestmethod, is it released the tomcat allocated threadnio-8080-exec-8? I am telling based on the log. Is this thread released and go back to tomcat connection pool? - If the tomcat thread released then how the client is getting a response after the execution of
completableFutureis done? - Can you please describe how thread allocation occur when request come to tomcat server, and spring application.
Thanks in advance