Spring的ThreadPoolTaskExecutor是否阻塞了什么?
我们在 Spring(Tomcat 内部)中使用 ThreadPoolTaskExecutor 来启动一个监听端口 (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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论