Search code examples
javatomcatasynchronoussynchronizationnio

How sync and async request processing differs in Tomcat?


I cannot figure out what difference is between sync and async calls in Tomcat. Everywhere I use NIO. I have thousand connections managed by few Tomcat threads. When long sync request incomes a thread borrows from Tomcat thread pool and processes the request. This thread waits for long process to be completed and then writes result to HTTPResponse. So resources are wasted just for awaiting. When long async request incomes then Tomcat thread creates separate thread and long process starts within this new thread and Tomcat thread returns to pool alomost immedately. Am i understood right? If so I don't see any difference between sync and async modes because in both modes same amount of threads is used


Solution

  • The difference is "pull" versus "push". Yes, you are correct, either way a thread must be allocated for doing the work.

    But with sync request you would have to create the worker thread manually and poll the task result from client, whereas with async the server will push the result to the client when the task completes.

    The latter is slightly more efficient because your server doesn't have to process many poll requests per result.