3

I have the following simple JSP running in Tomcat 7. Nothing else in the container and no web.xml. I hit the url http://localhost:8090/test/test.jsp three times in quick succession from my browser in three separate tabs (Chrome).

<%@ page import="java.util.Date" %>
<%
    out.println("Hello there dude");
    System.out.println("Hello there my friend "+ new Date() +"
                "+Thread.currentThread().getName());
    try {
        Thread.sleep(5000);
    } catch(Exception e)
    {
        e.printStackTrace();
    } 
%>

`

When I run it in Tomcat 7 or any other version of Tomcat, the JSP servlet will block multiple requests and I get output like this in the console.

Hello there my friend Thu Feb 02 19:31:35 MST 2012 http-bio-8090-exec-1
Hello there my friend Thu Feb 02 19:31:40 MST 2012 http-bio-8090-exec-3
Hello there my friend Thu Feb 02 19:31:45 MST 2012 http-bio-8090-exec-4

If you examine the times, you will see the JSP servlets are executing serially. I started them all at the same time, so I believe they should finish within a second of each other, but the subsequent requests don't start until the previous request has completed. These are start times above and the browser will hang on the last request for 15 seconds. The JSP requests should execute in parallel if I understand the spec as I am not asking for single threaded behavior.

Interesting enough, Tomcat is allocating different threads as shown above, but they are definitely executing serially. It's like the container won't release a new JSP servlet thread to process until the proceeding request is finished. We run Webservices all day and they seem to execute in parallel just fine.

I am running on a multi-core Windows box and the default out of the box Tomcat configuration which has 200 threads available.

dsolimano
  • 8,870
  • 3
  • 48
  • 63
Mastering_the_Object
  • 1,023
  • 1
  • 7
  • 13
  • 1
    In some browsers (I have tested Chrome and Firefox), the requests for multiple tabs use the same http port request and the browser is blocked from sending multiple requests. Ironically, I did not see this in ie9 for 3 or fewer tabs. So be careful of browsers as they may block your requests and the results may lead you to believe in an erroneous server behavior when it is really the client – Mastering_the_Object Feb 03 '12 at 03:43
  • possible duplicate of [servlet multithreading](http://stackoverflow.com/questions/7681291/servlet-multithreading) and [Java Servlet concurrent request](http://stackoverflow.com/questions/8011138/java-servlet-concurrent-request) – BalusC Feb 03 '12 at 20:02

1 Answers1

3

It does seem likely that this is actually a case of the browser blocking the requests, rather than Tomcat blocking threads. I just tried the same code on my Tomcat 7 install, and ran wget localhost:8060/ThreadTest/ & three times in rapid succession, and all three finished within 1 second of each other.

EMS
  • 178
  • 1
  • 9