0

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:

synchronized(obj){
 try{  
 obj.wait(50000)
   }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.

Pat928
  • 49
  • 7

2 Answers2

1

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 {  
           obj.wait(50000)
       } catch (InterruptedException e) {
   }
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
0

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:

    System.out.println(Thread.currentThread());
    try {
        Thread.sleep(50000);
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    }

And see the result.

dabaicai
  • 999
  • 8
  • 9