Spring的ThreadPoolTask​​Executor是否阻塞了什么?

发布于 2024-09-25 08:02:29 字数 1526 浏览 10 评论 0原文

我们在 Spring(Tomcat 内部)中使用 ThreadPoolTask​​Executor 来启动一个监听端口 (3001) 的并发服务器。

我们的代码看起来有点像:

        ....
        ServerSocket listener = new ServerSocket(port);
        Socket server;

        while (true) {
            PollingTask pollingTask;

            server = listener.accept();
            log.info ("Got a new connection, spawning a thread: " + i++);
            taskExecutor.execute(new PollingTask(server, i));
        }

PollingTask 的代码类似于:

   ....
   PollingTask(Socket server, int counter) {
        this.server = server;
        this.counter = counter;
    }

   public void run() {
        input = "";
        try {
            log.info ("New runnable thread: " + counter);
        }
   }
   ....

Spring 配置看起来像:

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
   <property name="corePoolSize" value="100" />
   <property name="maxPoolSize" value="200" />
   <property name="queueCapacity" value="50" />
   <property name="keepAliveSeconds" value="600" />
</bean>

这个特定的 bean 在启动时启动,然后我们尝试连接到端口 (3001) ,使用远程登录。

有趣的是,我们在日志中看到:

  Got a new connection, spawning a thread: 0
  ....

然而,在实际显示之前,它一直达到大约 48:

  New runnable thread: 0
  ....

如何说服 Spring 立即处理线程/任务,而不是等待?

(我尝试使用不同的 queueCapacity 值)

We are using the ThreadPoolTaskExecutor within Spring (inside of Tomcat) to launch a concurrent server which listens on a port (3001).

Our code looks a bit like:

        ....
        ServerSocket listener = new ServerSocket(port);
        Socket server;

        while (true) {
            PollingTask pollingTask;

            server = listener.accept();
            log.info ("Got a new connection, spawning a thread: " + i++);
            taskExecutor.execute(new PollingTask(server, i));
        }

And the code for PollingTask is similar to:

   ....
   PollingTask(Socket server, int counter) {
        this.server = server;
        this.counter = counter;
    }

   public void run() {
        input = "";
        try {
            log.info ("New runnable thread: " + counter);
        }
   }
   ....

The Spring configuration looks like:

<bean id="taskExecutor" class="org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor">
   <property name="corePoolSize" value="100" />
   <property name="maxPoolSize" value="200" />
   <property name="queueCapacity" value="50" />
   <property name="keepAliveSeconds" value="600" />
</bean>

This particular bean is started on start up, and then we try to connect to the port (3001), using telnet.

Interestingly, we see, in our logs:

  Got a new connection, spawning a thread: 0
  ....

However, it gets all the way to about 48 before it actually shows:

  New runnable thread: 0
  ....

How do I convince Spring to work on the thread / task immediately, rather than waiting?

(I've tried with varying values for queueCapacity)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文