如何在队列排队之前让ThreadPoolExecutor将线程增加到最大数量

    public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue workQueue,
                              ThreadFactory threadFactory,
                              RejectedExecutionHandler handler);
序号 参数名称 参数类型 参数含义
1 corePoolSize int 核心线程池大小
2 maximumPoolSize int 最大线程池大小
3 keepAliveTime long 线程最大空闲时间
4 unit TimeUnit 时间单位
5 workQueue BlockingQueue 线程等待队列
6 threadFactory ThreadFactory 线程创建工厂
7 handler RejectedExecutionHandler 拒绝策略

下面是一些源代码的实现:

    public void execute(Runnable command) {
        if (command == null)
            throw new NullPointerException();
​
        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);
    }

ArrayBlockingQueue.offer(E e)

    public boolean offer(E e) {
        checkNotNull(e);
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            if (count == items.length)
                return false;
            else {
                enqueue(e);
                return true;
            }
        } finally {
            lock.unlock();
        }
    }
​

我们可以总结出线程池默认的工作行为:

  • 不会初始化 corePoolSize 个线程,有任务来了才创建工作线程;

  • 当核心线程满了之后不会立即扩容线程池,而是把任务堆积到工作队列中;

  • 当工作队列满了后扩容线程池,一直到线程个数达到 maximumPoolSize 为止;

  • 如果队列已满且达到了最大线程后还有任务进来,按照拒绝策略处理;

  • 当线程数大于核心线程数时,线程等待 keepAliveTime 后还是没有任务需要处理的话,收缩线程到核心线程数。

了解这个策略,有助于我们根据实际的容量规划需求,为线程池设置合适的初始化参数。当然,我们也可以通过一些手段来改变这些默认工作行为,比如:

  • 声明线程池后立即调用 prestartAllCoreThreads 方法,来启动所有核心线程;

  • 传入 true 给 allowCoreThreadTimeOut 方法,来让线程池在空闲的时候同样回收核心线程。

不知道你有没有想过:Java 线程池是先用工作队列来存放来不及处理的任务,满了之后再扩容线程池。当我们的工作队列设置的很大时,最大线程数这个参数显得没有意义,因为队列很难满,或者到满的时候再去扩容线程池已经于事无补了。

 

那么,我们有没有办法让线程池更激进一点,优先开启更多的线程,而把队列当成一个后备方案呢?

下面我们自己实现一个ThreadPoolExecutor,重写某些方法。

public class MyThreadPoolExecutor extends ThreadPoolExecutor {
​
    /**
     * 构造方法
     *
     * @param corePoolSize    核心线程数
     * @param maximumPoolSize 最大线程数
     * @param keepAliveTime   非核心线程数保留时长
     * @param unit            非核心线程数保留时长单位
     * @param blockQueueSize  阻塞队列长度
     */
    public MyThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, int blockQueueSize) {
        super(corePoolSize, maximumPoolSize, keepAliveTime, unit, new MyThreadPoolExecutor.ExtremeBlockQueue<>(blockQueueSize), Executors.defaultThreadFactory(), new MyThreadPoolExecutor.ExtremePolicy());
​
    }
​
    /**
     * 自定义阻塞队列
     *
     * @param 
     */
    static class ExtremeBlockQueue extends LinkedBlockingQueue {
        public ExtremeBlockQueue(int capacity) {
            super(capacity);
        }
​
        /**
         * 覆盖默认的offer方法,触发拒绝策略执行
         *
         * @param e
         * @return
         */
        @Override
        public boolean offer(Runnable e) {
            if (size() == 0) {
                return super.offer(e);
            } else {
                return false;
            }
        }
​
    }
​
    /**
     * 自定义拒绝策略
     */
    static class ExtremePolicy implements RejectedExecutionHandler {
        @Override
        public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
            try {
                executor.getQueue().put(r);
                if (executor.isShutdown()) {
                    throw new RejectedExecutionException(
                            "Task " + r + " rejected from " + executor);
                }
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                return;
            }
        }
    }
}

我们创建一个自定义线程池

ExecutorService threadPool = new MyThreadPoolExecutor(1, 50, 5, TimeUnit.SECONDS,  Integer.MAX_VALUE);

通过这种机制,当我将任务提交到队列时,ThreadPoolExecutor将:

  1. 最初将线程数扩展到核心大小(此处为1)。

  2. 将其提供给队列。如果队列为空,它将排队等待由现有线程处理。

  3. 如果队列中已经有1个或多个元素,offer(...)将返回false。

  4. 如果返回false,则按比例扩大池中的线程数量,直到达到最大数量(此处为50)。

  5. 如果达到最大值,则调用 RejectedExecutionHandler

  6. RejectedExecutionHandler随后会将任务到队列通过FIFO顺序交给第一个可用线程处理。

尽管在上面的示例代码中,队列是无界的,但是您也可以将其定义为有界队列。例如,如果将容量添加到1000,LinkedBlockingQueue则它将:

  1. 将线程放大到最大

  2. 然后排队直到完成1000个任务

  3. 然后阻塞,直到队列可用为止。

 

我们还可以使用一个最简单的方法,将核心线程大小和最大线程大小设置为相同的值,并使用允许从池中删除核心线程allowCoreThreadTimeOut(true)

 

下面我们写一个demo演示一下:

我们创建一个Spring项目,用户在浏览器调用该方法:

    @GetMapping("right")
    public int right() throws InterruptedException {
        AtomicInteger atomicInteger = new AtomicInteger();
        ThreadPoolExecutor threadPool = new MyThreadPoolExecutor(
                2, 5,
                5, TimeUnit.SECONDS,10);
        printStats(threadPool);
        IntStream.rangeClosed(1, 20).forEach(i -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int id = atomicInteger.incrementAndGet();
            try {
                threadPool.submit(() -> {
                    log.info("{} started", id);
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e) {
                    }
                    log.info("{} finished", id);
                });
            } catch (Exception ex) {
                log.error("error submitting task {}", id, ex);
                atomicInteger.decrementAndGet();
            }
        });
​
        TimeUnit.SECONDS.sleep(60);
        return atomicInteger.intValue();
    }
​
    /**
     * 定时任务每秒打印当前线程池的状态
     *
     * @param threadPool
     */
    private void printStats(ThreadPoolExecutor threadPool) {
        Executors.newSingleThreadScheduledExecutor().scheduleAtFixedRate(() -> {
            log.info("=========================");
            log.info("Pool Size: {}", threadPool.getPoolSize());
            log.info("Active Threads: {}", threadPool.getActiveCount());
            log.info("Number of Tasks Completed: {}", threadPool.getCompletedTaskCount());
            log.info("Number of Tasks in Queue: {}", threadPool.getQueue().size());
​
            log.info("=========================");
        }, 0, 1, TimeUnit.SECONDS);
    }

下面是日志的一部分输出:

[16:33:43.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:43.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 0
[16:33:43.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 0
[16:33:43.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:43.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 0
[16:33:43.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:44.453] [pool-5-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 1 started
[16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 1
[16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 1
[16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 0
[16:33:44.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:45.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:45.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 2
[16:33:45.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 2
[16:33:45.453] [pool-5-thread-2] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 2 started
[16:33:45.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:45.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 0
[16:33:45.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:46.456] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:46.456] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 2
[16:33:46.456] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 2
[16:33:46.457] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:46.457] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 0
[16:33:46.457] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 2
[16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 2
[16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 1
[16:33:47.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:47.457] [pool-5-thread-3] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 4 started
[16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 3
[16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 3
[16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 1
[16:33:48.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:48.457] [pool-5-thread-4] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 5 started
[16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 4
[16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 4
[16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 1
[16:33:49.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:49.458] [pool-5-thread-5] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 6 started
[16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 1
[16:33:50.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 2
[16:33:51.453] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 3
[16:33:52.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 0
[16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 4
[16:33:53.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:54.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:54.454] [pool-5-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95  ] - 1 finished
[16:33:54.454] [pool-5-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 3 started
[16:33:54.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:33:54.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:33:54.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 1
[16:33:54.456] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 4
[16:33:54.456] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:55.454] [pool-5-thread-2] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95  ] - 2 finished
[16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:33:55.454] [pool-5-thread-2] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 7 started
[16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 2
[16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 4
[16:33:55.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 2
[16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 5
[16:33:56.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 2
[16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 6
[16:33:57.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:57.457] [pool-5-thread-3] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95  ] - 4 finished
[16:33:57.457] [pool-5-thread-3] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 8 started
[16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 3
[16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 6
[16:33:58.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:58.458] [pool-5-thread-4] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95  ] - 5 finished
[16:33:58.458] [pool-5-thread-4] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 9 started
[16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 4
[16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 6
[16:33:59.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:33:59.458] [pool-5-thread-5] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95  ] - 6 finished
[16:33:59.458] [pool-5-thread-5] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 10 started
[16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 5
[16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 6
[16:34:00.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 5
[16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 7
[16:34:01.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 5
[16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 8
[16:34:02.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 5
[16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 9
[16:34:03.454] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 5
[16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 10
[16:34:04.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:28  ] - =========================
[16:34:04.455] [pool-5-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95  ] - 3 finished
[16:34:04.455] [pool-5-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 11 started
[16:34:05.454] [pool-5-thread-2] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:95  ] - 7 finished
[16:34:05.454] [pool-5-thread-2] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:90  ] - 12 started
[16:34:05.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:22  ] - =========================
[16:34:05.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:23  ] - Pool Size: 5
[16:34:05.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:24  ] - Active Threads: 5
[16:34:05.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:25  ] - Number of Tasks Completed: 7
[16:34:05.455] [pool-6-thread-1] [INFO ] [o.g.t.c.t.t.ThreadPoolOOMController:26  ] - Number of Tasks in Queue: 8

可以看到线程池的线程数量会先从0增加到5,然后当线程数到达最大线程数时,后面的线程会进入到队列中排队等待

你可能感兴趣的:(java-web学习)