java - Java ThreadPoolExecutor 不创建新线程
问题描述
我在 AEM(Adobe Experience Manager)中有一个 servlet,它为每个请求创建一个新线程。我使用 Apache Sling 线程池来管理线程。一切正常,但如果 servlet 在几秒钟内收到数百个请求,则不会创建线程并且线程池将被转换为无用。我必须将 AEM 实例重新启动到线程池线程池使用无限制的队列,核心池包含十个元素。配置如下:
调试 java.util.concurrent.ThreadPoolExecutor 类,我的代码进入第三个“if”,然后没有进入 if 也没有进入 else,所以,线程没有被创建。
public void execute(Runnable command) {
if (command == null)
throw new NullPointerException();
/*
* Proceed in 3 steps:
*
* 1. If fewer than corePoolSize threads are running, try to
* start a new thread with the given command as its first
* task. The call to addWorker atomically checks runState and
* workerCount, and so prevents false alarms that would add
* threads when it shouldn't, by returning false.
*
* 2. If a task can be successfully queued, then we still need
* to double-check whether we should have added a thread
* (because existing ones died since last checking) or that
* the pool shut down since entry into this method. So we
* recheck state and if necessary roll back the enqueuing if
* stopped, or start a new thread if there are none.
*
* 3. If we cannot queue task, then we try to add a new
* thread. If it fails, we know we are shut down or saturated
* and so reject the task.
*/
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) {
if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) {
int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}
我阅读了文档,但不明白为什么会发生这种情况。
解决方案
推荐阅读
- python - 如何将坐标设置为状态空间(范围)以在 Q 表中使用?
- c++ - 输出说明
- azure-devops - 天蓝色回购分支政策
- data-visualization - 多变量颜色图和可视化 QGIS
- docker - How to setup docker registry in k8s cluster?
- prose-mirror - ProseMirror/vue-tiptap 替换 nbsp 到空间
- javascript - 如何制作一个检查数组值是否反向连续的javascript程序?
- mongodb - mongoDb 一次性获取过滤器数组
- python - 烧瓶返回空的json
- android - 选择 OpenCV sdk java 时看不到 OpenCV 模块名称