我知道Servlet请求默认是多线程的.我使用NetBeans创建了一个简单的servlet,它在Tomcat和JBoss上看起来都是单线程的.
我用这段代码测试了它:
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());
}
Run Code Online (Sandbox Code Playgroud)
(从doGet调用processRequest)
我几乎同时在我的浏览器中的2个选项卡中访问了这个,如果多线程可以工作,我希望它用两个不同的线程ID打印"访问",然后两个线程的"退出".相反,我得到这个输出:
14:53:41,839 INFO [stdout](http - 127.0.0.1-8080-1)access processRequest:OctaveServlet @ 31ccfe threadID:34 14:53:46,840 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)退出processRequest:OctaveServlet @ 31ccfe threadID:34
你可以看到它只是一个线程.不用说,我没有实现SingleThreadModel.
以下是我的系统的详细信息:NetBeans 7.0.1,JVM:Sun java 1.6.0_26,Tomcat 7.0.14,JBoss AS 7,Ubuntu 11.04
非常感谢任何帮助,
俄德.
它每个HTTP连接使用一个线程(当服务器使用NIO时,并不完全是这样,但你明白了这一点).您的浏览器显然在两个选项卡中使用相同的HTTP连接.产生两个不同的浏览器实例(例如Firefox和Chrome),您将看到它按预期方式工作.
| 归档时间: |
|
| 查看次数: |
2410 次 |
| 最近记录: |