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中的线程池技术,在很多 ...
随机推荐
- Qt5.7 + VS2015 环境搭建
http://blog.csdn.net/liang19890820/article/details/53931813#安装插件 配置 Qt 5.7 选择:Qt VS Tools -> Qt O ...
- oracle备份信息查询
SELECT TRIM(START_TIME || '#'), TRIM(END_TIME || '#'), TRIM(CASE OUTPUT_DEVICE_TYPE ...
- matlab fspecial
Matlab 的fspecial函数用法 fspecial函数用于建立预定义的滤波算子,其语法格式为:h = fspecial(type)h = fspecial(type,para)其中type指定 ...
- 项目Alpha冲刺 2
作业描述 课程: 软件工程1916|W(福州大学) 作业要求: 项目Alpha冲刺(团队) 团队名称: 火鸡堂 作业目标: 进行github实战训练,锻炼团队合作能力 1.团队信息 队名:火鸡堂 队员 ...
- 理解Express express.static 和 __direname 及 __firename的含义
理解Express express.static 和 __direname 及 __firename的含义 一:理解 app.use(express.static(__direname + '/pub ...
- 软概(lesson 1):Javaweb实现用户登录界面
一.问题描述 二.网站系统开发所需要的技术 网站界面开发:html 后台所需要的技术:java基本内容,数据库语句,连接数据库实现增删改查 本题所用技术:数据库链接以及增加功能,基本html语句 技术 ...
- TCP/IP与OSI模型
- TravelPort官方API解读
TravelPort Ping通使用教程 Unit1 Lesson 1: 标签(空格分隔): 完成第1单元的三个课程后,您可以使用Travelport Universal API来提出服务请求并了解响 ...
- Codeforces Hello 2019
Hello 2019 手速场qwq 反正EGH太神仙了啊.jpg 考试的时候不会啊.jpg A 暴力.jpg #include <cstdio> #include <algorith ...
- 3.1《想成为黑客,不知道这些命令行可不行》(Learn Enough Command Line to Be Dangerous)——下载文件
首先,为了不手动创建一个长文件(这太麻烦了),我们将使用强大的curl(有时也写作"cURL")工具从网上下载一个文件,这个命令可以让命令行与URL交互.尽管这不是Unix核心命令 ...