據我所知,Servlet請求默認爲多線程。我使用NetBeans創建了一個簡單的servlet,並且它在Tomcat和JBoss上看起來都是單線程的。servlet多線程
我測試了它使用此代碼:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("access processRequest: " + this + " threadID: " + Thread.currentThread().getId());
try {
Thread.sleep(5000);
} catch (InterruptedException ex) {
Logger.getLogger(OctaveServlet.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("exit processRequest: " + this + " threadID: " + Thread.currentThread().getId());
}
(的processRequest從的doGet調用)
我訪問這個從我的瀏覽器2名的標籤幾乎在同一時間,如果多線程會工作,我希望它打印出具有2個不同線程ID的「訪問」,然後是兩個線程的「退出」。 相反,我得到這樣的輸出:
14:53:41839 INFO [標準輸出](HTTP - 127.0.0.1-8080-1)訪問的processRequest:OctaveServlet @ 31ccfe線程ID:34 14:53:46840 INFO [ stdout](http - 127.0.0.1-8080-1)exit processRequest:OctaveServlet @ 31ccfe threadID:34 14:53:46,867 INFO [stdout](http - 127.0.0.1-8080-1)access processRequest:OctaveServlet @ 31ccfe threadID:34 14:53:51,867 INFO [stdout](http - 127.0.0.1-8080-1)exit processRequest:OctaveServlet @ 31ccfe threadID:34
正如你所看到的,它只是一個線程。不用說,我沒有實現SingleThreadModel。
這裏是我的系統的詳細信息: 的NetBeans 7.0.1,JVM:SUN JAVA 1.6.0_26下,Tomcat 7.0.14,JBoss應用服務器7,Ubuntu的11.04
任何幫助非常感謝,
俄德。
謝謝!不會猜到瀏覽器是罪魁禍首! – OdedS