I am wondering how Tomcat 7 implements async processing. I understand that the request thread returns immediately, allowing the request thread to immediately listen for a new request and respond to it.
How is the 'async' request handled? Is there a separate thread pool that handles async requests? I assume blocking IO is handled using something like java.nio.Selector for performance. What about threads that are blocking on CPU calculations?
You are mixing up different concepts. You must distinguish between:
ExecutorService
and use it to further process the request or you can create a newRunnable
and submit it to the obtainedAsyncContext
by callingAsyncContext.start()
. In case of Tomcat the latter approach uses Tomcat's thread pool defined inserver.xml
.The below example outlines how it can work. It uses only one thread for worker jobs. If you run it from 2 different browsers in parallel the output looks like this (I use a custom logger):
You see that the
doGet
methods immediately finish, whereas the worker still runs. The 2 test requests:http://localhost:8080/pc/TestServlet?name=chrome
andhttp://localhost:8080/pc/TestServlet?name=firefox
.Simple example Servlet
...and the TestWorker