java - 在 Glassfish v3 中,Servlet 请求无明显原因地按顺序执行

标签 java servlets multithreading jakarta-ee glassfish-3

我正在使用 Glassfish 3 Web 配置文件,无法让 http 工作线程在 servlet 上同时执行请求。

这就是我观察问题的方式。我制作了一个非常简单的 servlet,它将当前线程名称写入标准输出并 hibernate 10 秒:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println(Thread.currentThread().getName());
    try {
        Thread.sleep(10000); // 10 sec
    }
    catch (InterruptedException ex) {
    }
}

当我同时运行多个请求时,我在日志中清楚地看到请求是按顺序执行的(每 10 秒一次跟踪)。

INFO: http-thread-pool-8080-(2)
(10 seconds later...)
INFO: http-thread-pool-8080-(1)
(10 seconds later...)
INFO: http-thread-pool-8080-(2)

等等

我的所有 GF 设置都未受影响 - 这是开箱即用的配置(如果我没记错的话,默认线程池最少 2 个线程,最多 5 个线程)。

我真的不明白为什么 sleep() 会阻塞所有其他工作线程。任何见解将不胜感激!

最佳答案

克里斯在评论中明确指出了这一点。我复制了你的servlet,测试如下:

package com.stackoverflow.q2755338;

import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class Test {

    public static void main(String... args) throws Exception {
        // Those are indeed called sequentially.
        System.out.println("Starting to fire 3 requests in current thread...");
        new TestURL().run();
        new TestURL().run();
        new TestURL().run();
        System.out.println("Finished firing 3 requests in current thread!");

        // But those are called three at once.
        System.out.println("Starting to fire 3 requests in each its own thread...");
        ExecutorService executor = Executors.newFixedThreadPool(3);
        executor.submit(new TestURL());
        executor.submit(new TestURL());
        executor.submit(new TestURL());
        System.out.println("Finished firing 3 requests in each its own thread!");
        executor.shutdown();
    }

}

class TestURL implements Runnable {

    @Override
    public void run() {
        try {
            System.out.println("Firing request...");
            new URL("http://localhost:8181/JavaEE6/test").openStream();
            System.out.println("Request finished!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

服务器端的结果是:

INFO: start: http-thread-pool-8181-(2)
(10 seconds)
INFO: end: http-thread-pool-8181-(2)
INFO: start: http-thread-pool-8181-(1)
(10 seconds)
INFO: end: http-thread-pool-8181-(1)
INFO: start: http-thread-pool-8181-(2)
(10 seconds)
INFO: end: http-thread-pool-8181-(2)

INFO: start: http-thread-pool-8181-(1)
INFO: start: http-thread-pool-8181-(2)
INFO: start: http-thread-pool-8181-(3)
(10 seconds)
INFO: end: http-thread-pool-8181-(1)
INFO: end: http-thread-pool-8181-(2)
INFO: end: http-thread-pool-8181-(3)

关于java - 在 Glassfish v3 中,Servlet 请求无明显原因地按顺序执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2755338/

相关文章:

java - 如何使用 ps -axl 找到在 Linux 上运行的 Java 线程?

java - 序列化嵌套的 JSON 数组

java - 如何从 Java 代码调用 servlet

jsp - Servlet 的 doGet 不是从 JSP 表单调用的

Python 性能 - 最佳并行方法

java - 创建另一个线程

java - 如何防止 ProgressDialog 在 Android 中取消屏幕旋转更改?

java - 使用 eclipse 在我的控制台(系统输出)上无法使用特殊字符

java - Spring Caching - 如何管理不同范围的缓存?

java - JSP表单发送数据,包括servlet中的文件