首页 > 解决方案 > 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);
}

我阅读了文档,但不明白为什么会发生这种情况。

标签: javamultithreadingaemsling

解决方案


推荐阅读