如何在队列排队之前让ThreadPoolExecutor将线程增加到最大数量
public ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue<Runnable> 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 <Runnable>*/static class ExtremeBlockQueue<Runnable> extends LinkedBlockingQueue<Runnable> {public ExtremeBlockQueue(int capacity) {super(capacity);}
/*** 覆盖默认的offer方法,触发拒绝策略执行** @param e* @return*/@Overridepublic boolean offer(Runnable e) {if (size() == 0) {return super.offer(e);} else {return false;}}
}
/*** 自定义拒绝策略*/static class ExtremePolicy implements RejectedExecutionHandler {@Overridepublic 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个或多个元素,
offer(...)
将返回false。如果返回false,则按比例扩大池中的线程数量,直到达到最大数量(此处为50)。
如果达到最大值,则调用
RejectedExecutionHandler
在
RejectedExecutionHandler
随后会将任务到队列通过FIFO顺序交给第一个可用线程处理。
尽管在上面的示例代码中,队列是无界的,但是您也可以将其定义为有界队列。例如,如果将容量添加到1000,LinkedBlockingQueue
则它将:
将线程放大到最大
然后排队直到完成1000个任务
然后阻塞,直到队列可用为止。
我们还可以使用一个最简单的方法,将核心线程大小和最大线程大小设置为相同的值,并使用允许从池中删除核心线程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,然后当线程数到达最大线程数时,后面的线程会进入到队列中排队等待
如何在队列排队之前让ThreadPoolExecutor将线程增加到最大数量相关推荐
- 【Java 并发编程】线程池机制 ( 线程池阻塞队列 | 线程池拒绝策略 | 使用 ThreadPoolExecutor 自定义线程池参数 )
文章目录 一.线程池阻塞队列 二.拒绝策略 三.使用 ThreadPoolExecutor 自定义线程池参数 一.线程池阻塞队列 线程池阻塞队列是线程池创建的第 555 个参数 : BlockingQ ...
- 随机模拟在多排服务器上的应用,在Excel中应用随机函数模拟多服务台单队列排队系统...
在Excel中应用随机函数模拟多服务台单队列排队系统 [摘 要] 计算机模拟是研究排队论问题的一个重要手段,可以选择的软件有很多,本文提出了一种基于Excel的简便方法.选用Excel中的随机函数,配 ...
- Java队列、线程池及ThreadPoolExecutor自定义线程池实现
目录 1.阻塞队列 2.队列分类 3.API使用 4.线程池 4.1.线程池参数 4.2.线程池实现 4.3.任务执行流程 4.4.拒绝策略 4.5.参数合理值设置 5.自定义线程池流程 6.自定义线 ...
- ThreadPoolExecutor – Java线程池示例
Java thread pool manages the pool of worker threads. It contains a queue that keeps tasks waiting to ...
- Java多线程(三):使用ThreadPoolExecutor创建线程池
文章目录 1. 简介 2. 构造函数说明 3. ThreadPoolExecutor例子 4. 拒绝策略 4.1 CallerRunsPolicy 4.2 AbortPolicy 4.3 Discar ...
- java 队列已满_java – ThreadPoolExecutor当队列已满时阻塞?
在一些非常狭窄的情况下,您可以实现一个java.util.concurrent.RejectedExecutionHandler,它可以满足您的需要. RejectedExecutionHandler ...
- DS栈+队列—排队游戏
目录 题目描述 思路分析 AC代码 题目描述 在幼儿园中,老师安排小朋友做一个排队的游戏.首先老师精心的把数目相同的小男孩和小女孩编排在一个队列中,每个小孩按其在队列中的位置发给一个编号(编号从0开始 ...
- php前面有人排队,PHP实现打印类(实现队列排队打印)
class Wprint{ //收集打印代码 private $data = array(); //处理打印代码 private $handle; public function __construc ...
- java构建内存池队列_池化技术(线程池、连接池、内存池等)
一.池化技术 - 简单点来说,就是提前保存大量的资源,以备不时之需. 对于线程,内存,oracle的连接对象等等,这些都是资源,程序中当你创建一个线程或者在堆上申请一块内存时,都涉及到很多系统调用,也 ...
最新文章
- 京东物流基于Doris的亿级数据自助探索应用
- 2.6 多分类问题-机器学习笔记-斯坦福吴恩达教授
- 成功解决SQL Server软件中出现的18470问题
- SpringCloud运行时刷新数据源相关配置
- 软件测试——Calculator测试
- C#使用 System.Net.Mail发送邮件功能
- java全文检索工具_全文检索工具elasticsearch:第三章: Java程序中的应用
- 2018怎么更换图框_2018 乐博睿 全年回顾
- oracle通过数据泵导出数据,Oracle通过PL/SQL数据泵导出导入数据的命令
- Powershell进阶学习(1) 浅谈Powershell学习方法
- 视易精通收银服务器自动关机,视易精通量贩式收银系统操作手册3.0
- Linux命令中CVf参数代表什么,Linux tar 命令使用参数详解
- FTP Server端的使用
- mysql自动生成id方式_Mysql全局ID生成方法
- 基于深度学习的高精地图的自动生成与标注
- 使用python判断成绩是否合格
- 计算机c盘用户爆满,电脑c盘突然爆满是为什么!?
- 2、虚拟机的快照与克隆
- 一组基于SVG矢量图库和jQuery/GSAP的精美图标动画
- 嵌入式算法:STM32 F4xx 使用DSP实现cubic spline插值算法