ThreadPoolExecutor参数
1、ThreadPoolExecutor个参数的意义(类上的注释内容)
* @param corePoolSize the number of threads to keep in the
* pool, even if they are idle.
* @param maximumPoolSize the maximum number of threads to allow in the
* pool.
* @param keepAliveTime when the number of threads is greater than
* the core, this is the maximum time that excess idle threads
* will wait for new tasks before terminating.
* @param unit the time unit for the keepAliveTime
* argument.
* @param workQueue the queue to use for holding tasks before they
* are executed. This queue will hold only the <tt>Runnable</tt>
* tasks submitted by the <tt>execute</tt> method.
* @param threadFactory the factory to use when the executor
* creates a new thread.
* @param handler the handler to use when execution is blocked
* because the thread bounds and queue capacities are reached.
2、我对参数的理解(重要)
- corePoolSize:核心线程数,线程池创建时,就会创建的线程数。所有线程都执行完后,核心线程依然会保持。
- maximumPoolSize:最大的线程数。线程池最多会初始化maximumPoolSize个线程。
- keepAliveTime:idle线程存活时间。
- unit:keepAliveTime对应的时间单位。
- workQueue:队列,如果任务数已经达到corePoolSize,再向线程池中加入任务时,会将任务放到workQueue中。可以使用LinkedBlockingQueue,队列的数量没有限制。在构造BlockingQueue的时候,也可以传入最大值。如果有最大值,在queue中任务的数量达到最大值,再向线程池中加入任务时,就会启动新的线程,直到线程池中线程的数量达到maximumPoolSize为止。再向线程池中加入线程,会报错。
- threadFactory:新线程的构造器。
- handler:如果抛出异常,异常处理的Handler。
综上:线程池中的线程数量在线程池初始化的时候,为corePoolSize;随着加入线程池的任务数量增加, 运行的线程并不会增加,无法运行的任务会放到workQueue队列中;当workQueue队列已经放满了任务,新加入的任务会再启动新的线程来执行,直到正在运行的线程数量达到maximumPoolSize为止;再向线程池中添加任务,将会抛出异常。
3、验证各参数的代码
public static class ExecutorHelper {
public static String getInfo(ThreadPoolExecutor executor) {
StringBuilder sb = new StringBuilder();
sb.append("************** corePoolSize: " + executor.getCorePoolSize());
sb.append("************** poolSize: " + executor.getPoolSize());
sb.append("************** activeCount: " + executor.getActiveCount());
sb.append("************** completeTaskCount: " + executor.getCompletedTaskCount());
sb.append("************** largestPoolSize: " + executor.getLargestPoolSize());
sb.append("************** maximumPoolSize: " + executor.getMaximumPoolSize());
sb.append("************** taskCount: " + executor.getTaskCount());
sb.append("************** queueSize: " + executor.getQueue().size());
// sb.append("************** corePoolSize: " + executor.getCorePoolSize());
// sb.append("************** corePoolSize: " + executor.getCorePoolSize()); return sb.toString();
}
} public static class MyRunnable implements Runnable { private ThreadPoolExecutor executor;
public MyRunnable(ThreadPoolExecutor executor) {
this.executor = executor; System.out.println(ExecutorHelper.getInfo(executor));
} @Override
public void run() { Thread th = Thread.currentThread();
System.out.println(th.getName());
// System.out.println(ExecutorHelper.getInfo(executor)); try {
Thread.sleep(1000 * 30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws Exception {
ThreadPoolExecutor executor = new ThreadPoolExecutor(2,
5, 0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(4), Executors.defaultThreadFactory(),
new ThreadPoolExecutor.AbortPolicy()); for (int i = 0; i < 10; i++) {
Runnable runnable = new MyRunnable(executor);
executor.submit(runnable);
Thread.sleep(1000);
}
}
4、输出的内容:
************** corePoolSize: 2************** poolSize: 0************** activeCount: 0************** completeTaskCount: 0************** largestPoolSize: 0************** maximumPoolSize: 5************** taskCount: 0************** queueSize: 0
pool-2-thread-1
************** corePoolSize: 2************** poolSize: 1************** activeCount: 1************** completeTaskCount: 0************** largestPoolSize: 1************** maximumPoolSize: 5************** taskCount: 1************** queueSize: 0
pool-2-thread-2
************** corePoolSize: 2************** poolSize: 2************** activeCount: 2************** completeTaskCount: 0************** largestPoolSize: 2************** maximumPoolSize: 5************** taskCount: 2************** queueSize: 0
************** corePoolSize: 2************** poolSize: 2************** activeCount: 2************** completeTaskCount: 0************** largestPoolSize: 2************** maximumPoolSize: 5************** taskCount: 3************** queueSize: 1
************** corePoolSize: 2************** poolSize: 2************** activeCount: 2************** completeTaskCount: 0************** largestPoolSize: 2************** maximumPoolSize: 5************** taskCount: 4************** queueSize: 2
************** corePoolSize: 2************** poolSize: 2************** activeCount: 2************** completeTaskCount: 0************** largestPoolSize: 2************** maximumPoolSize: 5************** taskCount: 5************** queueSize: 3
************** corePoolSize: 2************** poolSize: 2************** activeCount: 2************** completeTaskCount: 0************** largestPoolSize: 2************** maximumPoolSize: 5************** taskCount: 6************** queueSize: 4
pool-2-thread-3
************** corePoolSize: 2************** poolSize: 3************** activeCount: 3************** completeTaskCount: 0************** largestPoolSize: 3************** maximumPoolSize: 5************** taskCount: 7************** queueSize: 4
pool-2-thread-4
************** corePoolSize: 2************** poolSize: 4************** activeCount: 4************** completeTaskCount: 0************** largestPoolSize: 4************** maximumPoolSize: 5************** taskCount: 8************** queueSize: 4
pool-2-thread-5
Exception in thread "main" java.util.concurrent.RejectedExecutionException
at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:1768)
************** corePoolSize: 2************** poolSize: 5************** activeCount: 5************** completeTaskCount: 0************** largestPoolSize: 5************** maximumPoolSize: 5************** taskCount: 9************** queueSize: 4
at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:767)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:658)
at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:78)
at com.jd.wms.inbound.service.socket.client.DhSocketClient.main(DhSocketClient.java:122)
pool-2-thread-1
pool-2-thread-2
pool-2-thread-3
pool-2-thread-4
ThreadPoolExecutor参数的更多相关文章
- 线程池ThreadPoolExecutor参数设置
线程池ThreadPoolExecutor参数设置 JDK1.5中引入了强大的concurrent包,其中最常用的莫过了线程池的实现ThreadPoolExecutor,它给我们带来了极大的方便,但同 ...
- ThreadPoolExecutor参数详解
ThreadPoolExecutor全部参数的构造函数 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long ke ...
- ThreadPoolExecutor参数解析
ThreadPoolExecutor是一个非常重要的类,用来构建带有线程池的任务执行器,通过配置不同的参数来构造具有不同规格线程池的任务执行器. 写在前面的是: 线程池和任务执行器,线程池的定义比较直 ...
- ThreadPoolExecutor参数讲解
1. 线程池可以节省创建多个线程带来的开销问题. 2. 线程池的参数如下: public ThreadPoolExecutor(int corePoolSize, int maximumPoolSiz ...
- ThreadPoolExecutor参数以及源码介绍
1.前言 在阿里巴巴的<Java 开发手册>中是这样规定线程池的: 线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写 ...
- 【并发编程】ThreadPoolExecutor参数详解
ThreadPoolExecutor executor = new ThreadPoolExecutor( int corePoolSize, int maximumPoolSize, long ke ...
- 线程池ThreadPoolExecutor参数分析
概述 比如去火车站买票, 有7个(maximumPoolSize)售票窗口, 但只有3个(corePoolSize)窗口对外开放.那么对外开放的3个窗口称为核心线程数, 而最大线程数是7个窗口. 如果 ...
- 线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式
1. 通过Executors创建线程池的弊端 在创建线程池的时候,大部分人还是会选择使用Executors去创建. 下面是创建定长线程池(FixedThreadPool)的一个例子,严格来说,当使用如 ...
- 高并发之——不得不说的线程池与ThreadPoolExecutor类浅析
一.抛砖引玉 既然Java中支持以多线程的方式来执行相应的任务,但为什么在JDK1.5中又提供了线程池技术呢?这个问题大家自行脑补,多动脑,肯定没坏处,哈哈哈... 说起Java中的线程池技术,在很多 ...
随机推荐
- document.documentElement.scrollTop(获取滚动条位置)
要获取当前页面的滚动条纵坐标位置,用:document.documentElement.scrollTop;而不是:document.body.scrollTop;documentElement 对应 ...
- (零 ) 天猫精灵接入Home Assistant-总说明
天猫精灵设备管理 https://bbs.hassbian.com/tmall 自己的hass访问地址 http://[自己的IP或域名]:8123/states 自己的MQTT服务器访问 http: ...
- 对node.js的理解?
a.Node.js是一个基于Google Chrome V8引擎的javascript运行环境.Node.js使用了一个事件驱动.非阻塞式I/O的模型,使其轻量又高效.Node.js的包管理器npm, ...
- SQL优化思路大全
一.百万级数据库优化方案 1.对查询进行优化,要尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2.应尽量避免在 where 子句中对字段进行 null 值判断 ...
- 【Codeforces 152E】Garden
Codeforces 152 E 题意:给你一个\(n\times m\)的格子,每个格子里面有一个值\(a_{i,j}\)表示如果要将这个格子变成路的话需要花费这么多代价.现在有\(k\)个特殊格子 ...
- 使用awk按照行数切割文件
最近在做一个事情,需要将一个文本文件按照行数进行切割,然后用了,awk的方法,感觉很好用, 记录一下. 脚本如下: #!/bin/bash ## 文件效果: 根据行数来切割文件 ## 参数1为要切割的 ...
- ionic访问odoo 11接口
在架设完毕odoo 11的网站之后,第一次面临手机app该如何访问后台网站的问题,是不是模式类似asp.net mvc 那样的模式,或者还存在其他的访问方法,带着这个疑问与困惑,开始的我的研究学习之路 ...
- Error creating bean with name 'enableRedisKeyspaceNotificationsInitializer' defined in class path resource
我们在SpringBoot中用Jedis来访问Redis,其中Redis是采用集群(单机没有本篇文章的问题)的方式,在启用Redis的时候碰到如上问题. 错误的核心信息如下: Error creati ...
- 算法相关——Java排序算法之桶排序(一)
(代码中对应一个数组的下标),将每个元素放入对应桶中,再将所有元素按顺序输出(代码中则按顺序将数组i下标输出arrary[i]次),即为{0,1,3,5,5,6,9}. 1.2 代码实现 /* *@ ...
- Luogu P1447 [NOI2010]能量采集
Preface 最近反演题做多了看什么都想反演.这道题由于数据弱,解法多种多样,这里简单分析一下. 首先转化下题目就是对于一个点\((x,y)\),所消耗的能量就是\(2(\gcd(x,y)-1)+1 ...