It seems that only one thread processes all requests in Servlet

This question already has an answer here:

  • Servlet seems to handle multiple concurrent browser requests synchronously

    1 answer

It is obvious that a servlet can process multiple requests at the same time. For example, a connection is made on the server (a socket), to process this connection the server creates a new thread, the thread calls doGet of the servlet and goes to do something. While it is doing something another request arrives. A new thread will call doGet with other httpRequest and httpResponse instances. I did a test using netbeans and glasfish. In the doGet of a servlet I put:

   }catch(InterruptedException e)

when the first request arrives in, the servlet does not accept any more requests until 50 seconds. It means that for the succeeding requests to be captured, the previous thread must do its job and return back to get another request. To be sure this is not because of synchronized statement i put a breakpoint on some statement before synchronized but never reached.

Servlet frameworks are normally multi-threaded.

I can think of two possible explanations:

  • Your client is single-threaded; i.e. your second request is not sent until the response from the first one is received.

  • Somehow you have configured GlassFish with only one worker thread.

It is also possible that you have been mislead by your testing to ensure that the problem isn’t the obj lock. Try changing it to use a local lock; e.g.

   Object obj = new Object();  // ensure nothing else can see it!!
   synchronized (obj) {
       try {  
       } catch (InterruptedException e) {

Normally, the servlet is multi-thread if you don’t change default configuration about the worker thread size in the servlet container.

In your case,according to the result in your description,i think the obj is a shared variable,like class variable,instance variable and so on.
So you have a mistake,your should not synchronized on the obj1 instance,because this will make it to be sequential.

You can change your test case like:

    try {
    } catch (InterruptedException e) {

And see the result.