Servlet 似乎同步处理多个并发浏览器请求 [英] Servlet seems to handle multiple concurrent browser requests synchronously

查看:24
本文介绍了Servlet 似乎同步处理多个并发浏览器请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Java Servlets 正在同时处理多个请求,我已经通过 StackOverflow 和 Google 进行了搜索,并证实了我的想法.但是我现在很困惑,我写了一个简单的 servlets,它似乎显示了阻塞行为.

As far as I know Java Servlets are handling multiple requests concurrently and I've searched through StackOverflow as well as Google, and confirmed what I thought. However I am quite confused right now, I wrote a simple servlets that seem to show blocking behaviour.

所以我有一个简单的 Servlet:

so I have a simple Servlet:

public class MyServlet extends HttpServlet 
{
    private static final long serialVersionUID = 2628320200587071622L;

    private static final Logger logger = Logger.getLogger(MyServlet.class);

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
    {
        logger.info("[doGet] Test before");

        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        logger.info("[doGet] Test after");

        resp.setContentType("text/plain");
        resp.getWriter().write("OK");

    }
}

然后我有 2 个浏览器窗口,我同时打开了我的 Servlet.结果是第一个请求阻塞了第二个请求.日志还显示:

Then I have 2 browser windows, I opened at the same time that hit my Servlet. And the result is the first request blocking the 2nd one. The log also shows:

10:49:05,088 [http-8383-Processor14]  INFO MyServlet - [doGet] Test before
10:50:05,096 [http-8383-Processor14]  INFO MyServlet - [doGet] Test after
10:50:05,106 [http-8383-Processor22]  INFO MyServlet - [doGet] Test before
10:51:05,112 [http-8383-Processor22]  INFO MyServlet - [doGet] Test after

我觉得我错过了一些东西...... Servlets 应该能够处理并发请求,但它似乎并没有这样做.我也在 service 方法上做了与上面相同的事情,而不是 doGet 并且它做了同样的事情.

I feel like I am missing something ... Servlets supposed to be able to handle concurrent request, but it doesnt seem to be doing it. I also did the same as above on the service method instead of doGet and it does the same thing.

有什么指点吗?

谢谢

推荐答案

您的浏览器显然在不同的窗口中使用相同的 HTTP 连接.servlet 容器为每个 HTTP 连接使用一个线程,而不是每个 HTTP 请求.您应该运行两个物理上不同的网络浏览器来正确测试.例如.一台 Firefox,一台 Chrome.

Your browser is apparently using the same HTTP connection in different windows. The servlet container uses a single thread per HTTP connection, not per HTTP request. You should run two physically different webbrowsers to test this properly. E.g. one Firefox and one Chrome.

这篇关于Servlet 似乎同步处理多个并发浏览器请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆