I have developed a simple server using Tomcat which runs a servlet.
The servlet calls a command line program – which takes about 20 seconds to execute then returns the result to the user via JSON. The problem is – if i make above 2 simultaneous requests, the servlet blocks until one of the previous requests is completed.
An example of this can be seen below – “Im in” is the top of the servlet, and the list of results is after the servlet is executed. All requests were made at the same time – but you can clearly see they are not dealt with simultaneously. What setting do I need to change in tomcat in order to have all requests handeled at the same time?
Im in Im in FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE Im in FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE Im in FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE Im in FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE Im in FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE Im in FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE Im in FVFNT01 STOP_IDLE FVFNT03 STOP_IDLE FVFNT16 STOP_IDLE FVFNT17 STOP_IDLE
Requests to servlets are handled concurrently by default. There is no setting that enables/disables this behaviour. This is confirmed by the JavaDoc for HttpServlet:
However, if your servlet implements the marker interface SingleThreadModel, the servlet will only handle one request at-a-time. However, use of this interface is generally considered a bad practice, and because you didn’t mention it, I assume you’re not using it.
Of course even if you’re not implementing
SingleThreadModelyou can make any servlet single-threaded using (probably inappropriate) synchronisation, e.g.In the example above, only one thread may execute the same request method at-a-time, though it is possible (for example) for one thread to execute
doPost()while another is executingdoGet().If you don’t understand why this is, then I recommend you do some reading on concurrent programming in Java before investigating your problem further.