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参数的更多相关文章

  1. 线程池ThreadPoolExecutor参数设置

    线程池ThreadPoolExecutor参数设置 JDK1.5中引入了强大的concurrent包,其中最常用的莫过了线程池的实现ThreadPoolExecutor,它给我们带来了极大的方便,但同 ...

  2. ThreadPoolExecutor参数详解

    ThreadPoolExecutor全部参数的构造函数 public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long ke ...

  3. ThreadPoolExecutor参数解析

    ThreadPoolExecutor是一个非常重要的类,用来构建带有线程池的任务执行器,通过配置不同的参数来构造具有不同规格线程池的任务执行器. 写在前面的是: 线程池和任务执行器,线程池的定义比较直 ...

  4. ThreadPoolExecutor参数讲解

    1. 线程池可以节省创建多个线程带来的开销问题. 2. 线程池的参数如下: public ThreadPoolExecutor(int corePoolSize, int maximumPoolSiz ...

  5. ThreadPoolExecutor参数以及源码介绍

    1.前言 在阿里巴巴的<Java 开发手册>中是这样规定线程池的: 线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写 ...

  6. 【并发编程】ThreadPoolExecutor参数详解

    ThreadPoolExecutor executor = new ThreadPoolExecutor( int corePoolSize, int maximumPoolSize, long ke ...

  7. 线程池ThreadPoolExecutor参数分析

    概述 比如去火车站买票, 有7个(maximumPoolSize)售票窗口, 但只有3个(corePoolSize)窗口对外开放.那么对外开放的3个窗口称为核心线程数, 而最大线程数是7个窗口. 如果 ...

  8. 线程池不允许使用Executors去创建,而是通过ThreadPoolExecutor的方式

    1. 通过Executors创建线程池的弊端 在创建线程池的时候,大部分人还是会选择使用Executors去创建. 下面是创建定长线程池(FixedThreadPool)的一个例子,严格来说,当使用如 ...

  9. 高并发之——不得不说的线程池与ThreadPoolExecutor类浅析

    一.抛砖引玉 既然Java中支持以多线程的方式来执行相应的任务,但为什么在JDK1.5中又提供了线程池技术呢?这个问题大家自行脑补,多动脑,肯定没坏处,哈哈哈... 说起Java中的线程池技术,在很多 ...

随机推荐

  1. go标准库的学习-crypto/aes

    参考:https://studygolang.com/pkgdoc 导入方式: import "crypto/aes" aes包实现了AES加密算法,参见U.S. Federal ...

  2. 理解HTML5中Range对象

    1.理解Range对象    重新来学习下HTML5中的Range对象和Selection对象,最近在维护富文本编辑器,感觉这方面的知识点很有用,所以趁着周末多学习下~  什么是Range对象? 在H ...

  3. 吴恩达机器学习CS229课程笔记学习

    监督学习(supervised learning) 假设我们有一个数据集(dataset),给出居住面积和房价的关系如下: 我们以居住面积为横坐标,房价为纵坐标,组成数据点,如(2104, 400), ...

  4. Android ScrollView和ListView联用,且ListView可以下拉刷新和上拉加载

    ScrollView嵌套listView且ListView可以实现上拉加载. 由于代码太长,在此只提供实现思路: 先不说上拉加载的事,咱们先回想一下,ScrollView和LsitView联用,时的解 ...

  5. MVC 5限制所有HTTP请求必须是POST方式

    今天有位同事,提出了这样一个问题,他想限制所有MVC接收到的HTTP请求必须是POST方式. 接下来在下面的内容中,将我想到的方式分享给大家,如果大家有其它的方式,请留言. 一.HttpPostAtt ...

  6. SkylineGlobe 如何二次开发获取三维模型的BBOX和设置Tint属性

    测试模型类型选择TerrainModel和Feature两种,测试代码如下: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transiti ...

  7. Luogu2612 ZJOI2012 波浪 DP

    传送门 花掉了自己用来搞学科的时间做了这道题-- 一道类似的题:Here 考虑拆开绝对值计算贡献.那么我们对于\(1\)到\(N\)的排列,从小到大地将插入它们插入排列中. 假设我们现在计算到了数\( ...

  8. cssie7.0兼容

    http://www.w3dev.cn/article/20140328/IE7-float-left-touch-border-inner-break.aspx

  9. CYJian的水题大赛

    实在没忍住就去打比赛了然后一耗就是一天 最后Rank19还是挺好的(要不是乐多赛不然炸飞),这是唯一一套在Luogu上号称水题大赛的而实际上真的是水题大赛的比赛 好了我们开始看题 T1 八百标兵奔北坡 ...

  10. Verilog对数据进行四舍五入(round)与饱和(saturation)截位

    转自https://www.cnblogs.com/liujinggang/p/10549095.html 一.软件平台与硬件平台 软件平台: 操作系统:Windows 8.1 64-bit 开发套件 ...