ThreadPoolExecutor机制 

一、概述 
1、ThreadPoolExecutor作为java.util.concurrent包对外提供基础实现,以内部线程池的形式对外提供管理任务执行,线程调度,线程池管理等等服务; 
2、Executors方法提供的线程服务,都是通过参数设置来实现不同的线程池机制。 
3、先来了解其线程池管理的机制,有助于正确使用,避免错误使用导致严重故障。同时可以根据自己的需求实现自己的线程池

二、核心构造方法讲解 
下面是ThreadPoolExecutor最核心的构造方法

构造方法参数讲解 

参数名 作用
corePoolSize 核心线程池大小
maximumPoolSize 最大线程池大小
keepAliveTime 线程池中超过corePoolSize数目的空闲线程最大存活时间;可以allowCoreThreadTimeOut(true)使得核心线程有效时间
TimeUnit keepAliveTime时间单位
workQueue 阻塞任务队列
threadFactory 新建线程工厂
RejectedExecutionHandler 当提交任务数超过maxmumPoolSize+workQueue之和时,任务会交给RejectedExecutionHandler来处理

重点讲解: 
其中比较容易让人误解的是:corePoolSize,maximumPoolSize,workQueue之间关系。

1.当线程池小于corePoolSize时,新提交任务将创建一个新线程执行任务,即使此时线程池中存在空闲线程。 
2.当线程池达到corePoolSize时,新提交任务将被放入workQueue中,等待线程池中任务调度执行 
3.当workQueue已满,且maximumPoolSize>corePoolSize时,新提交任务会创建新线程执行任务 
4.当提交任务数超过maximumPoolSize时,新提交任务由RejectedExecutionHandler处理 
5.当线程池中超过corePoolSize线程,空闲时间达到keepAliveTime时,关闭空闲线程 
6.当设置allowCoreThreadTimeOut(true)时,线程池中corePoolSize线程空闲时间达到keepAliveTime也将关闭

线程管理机制图示: 

三、Executors提供的线程池配置方案

1、构造一个固定线程数目的线程池,配置的corePoolSize与maximumPoolSize大小相同,同时使用了一个无界LinkedBlockingQueue存放阻塞任务,因此多余的任务将存在再阻塞队列,不会由RejectedExecutionHandler处理 

public static ExecutorService newFixedThreadPool(int nThreads) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());}


2、构造一个缓冲功能的线程池,配置corePoolSize=0,maximumPoolSize=Integer.MAX_VALUE,keepAliveTime=60s,以及一个无容量的阻塞队列 SynchronousQueue,因此任务提交之后,将会创建新的线程执行;线程空闲超过60s将会销毁 

public static ExecutorService newCachedThreadPool() {return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());}


3、构造一个只支持一个线程的线程池,配置corePoolSize=maximumPoolSize=1,无界阻塞队列LinkedBlockingQueue;保证任务由一个线程串行执行 

public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()));}


4、构造有定时功能的线程池,配置corePoolSize,无界延迟阻塞队列DelayedWorkQueue;有意思的是:maximumPoolSize=Integer.MAX_VALUE,由于DelayedWorkQueue是无界队列,所以这个值是没有意义的 

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {return new ScheduledThreadPoolExecutor(corePoolSize);}public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) {return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);}public ScheduledThreadPoolExecutor(int corePoolSize,ThreadFactory threadFactory) {super(corePoolSize, Integer.MAX_VALUE, 0, TimeUnit.NANOSECONDS,new DelayedWorkQueue(), threadFactory);}

四、定制属于自己的非阻塞线程池 

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;public class CustomThreadPoolExecutor {private ThreadPoolExecutor pool = null;/*** 线程池初始化方法* * corePoolSize 核心线程池大小----10* maximumPoolSize 最大线程池大小----30* keepAliveTime 线程池中超过corePoolSize数目的空闲线程最大存活时间----30+单位TimeUnit* TimeUnit keepAliveTime时间单位----TimeUnit.MINUTES* workQueue 阻塞队列----new ArrayBlockingQueue<Runnable>(10)====10容量的阻塞队列* threadFactory 新建线程工厂----new CustomThreadFactory()====定制的线程工厂* rejectedExecutionHandler 当提交任务数超过maxmumPoolSize+workQueue之和时,*                           即当提交第41个任务时(前面线程都没有执行完,此测试方法中用sleep(100)),*                                   任务会交给RejectedExecutionHandler来处理*/public void init() {pool = new ThreadPoolExecutor(10,30,30,TimeUnit.MINUTES,new ArrayBlockingQueue<Runnable>(10),new CustomThreadFactory(),new CustomRejectedExecutionHandler());}public void destory() {if(pool != null) {pool.shutdownNow();}}public ExecutorService getCustomThreadPoolExecutor() {return this.pool;}private class CustomThreadFactory implements ThreadFactory {private AtomicInteger count = new AtomicInteger(0);@Overridepublic Thread newThread(Runnable r) {Thread t = new Thread(r);String threadName = CustomThreadPoolExecutor.class.getSimpleName() + count.addAndGet(1);System.out.println(threadName);t.setName(threadName);return t;}}private class CustomRejectedExecutionHandler implements RejectedExecutionHandler {@Overridepublic void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {// 记录异常// 报警处理等System.out.println("error.............");}}// 测试构造的线程池public static void main(String[] args) {CustomThreadPoolExecutor exec = new CustomThreadPoolExecutor();// 1.初始化exec.init();ExecutorService pool = exec.getCustomThreadPoolExecutor();for(int i=1; i<100; i++) {System.out.println("提交第" + i + "个任务!");pool.execute(new Runnable() {@Overridepublic void run() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("running=====");}});}// 2.销毁----此处不能销毁,因为任务没有提交执行完,如果销毁线程池,任务也就无法执行了// exec.destory();try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}}
}


方法中建立一个核心线程数为30个,缓冲队列有10个的线程池。每个线程任务,执行时会先睡眠3秒,保证提交10任务时,线程数目被占用完,再提交30任务时,阻塞队列被占用完,,这样提交第41个任务是,会交给CustomRejectedExecutionHandler 异常处理类来处理。

提交任务的代码如下: 

public void execute(Runnable command) {if (command == null)throw new NullPointerException();/** Proceed in 3 steps:** 1. If fewer than corePoolSize threads are running, try to* start a new thread with the given command as its first* task.  The call to addWorker atomically checks runState and* workerCount, and so prevents false alarms that would add* threads when it shouldn't, by returning false.** 2. If a task can be successfully queued, then we still need* to double-check whether we should have added a thread* (because existing ones died since last checking) or that* the pool shut down since entry into this method. So we* recheck state and if necessary roll back the enqueuing if* stopped, or start a new thread if there are none.** 3. If we cannot queue task, then we try to add a new* thread.  If it fails, we know we are shut down or saturated* and so reject the task.*/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);}


注意:41以后提交的任务就不能正常处理了,因为,execute中提交到任务队列是用的offer方法,如上面代码,这个方法是非阻塞的,所以就会交给CustomRejectedExecutionHandler 来处理,所以对于大数据量的任务来说,这种线程池,如果不设置队列长度会OOM,设置队列长度,会有任务得不到处理,接下来我们构建一个阻塞的自定义线程池

五、定制属于自己的阻塞线程池 

package com.tongbanjie.trade.test.commons;import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;public class CustomThreadPoolExecutor {  private ThreadPoolExecutor pool = null;  /** * 线程池初始化方法 *  * corePoolSize 核心线程池大小----1 * maximumPoolSize 最大线程池大小----3 * keepAliveTime 线程池中超过corePoolSize数目的空闲线程最大存活时间----30+单位TimeUnit * TimeUnit keepAliveTime时间单位----TimeUnit.MINUTES * workQueue 阻塞队列----new ArrayBlockingQueue<Runnable>(5)====5容量的阻塞队列 * threadFactory 新建线程工厂----new CustomThreadFactory()====定制的线程工厂 * rejectedExecutionHandler 当提交任务数超过maxmumPoolSize+workQueue之和时, *                          即当提交第41个任务时(前面线程都没有执行完,此测试方法中用sleep(100)), *                                任务会交给RejectedExecutionHandler来处理 */  public void init() {  pool = new ThreadPoolExecutor(  1,  3,  30,  TimeUnit.MINUTES,  new ArrayBlockingQueue<Runnable>(5),  new CustomThreadFactory(),  new CustomRejectedExecutionHandler());  }  public void destory() {  if(pool != null) {  pool.shutdownNow();  }  }  public ExecutorService getCustomThreadPoolExecutor() {  return this.pool;  }  private class CustomThreadFactory implements ThreadFactory {  private AtomicInteger count = new AtomicInteger(0);  @Override  public Thread newThread(Runnable r) {  Thread t = new Thread(r);  String threadName = CustomThreadPoolExecutor.class.getSimpleName() + count.addAndGet(1);  System.out.println(threadName);  t.setName(threadName);  return t;  }  }  private class CustomRejectedExecutionHandler implements RejectedExecutionHandler {  @Override  public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {  try {// 核心改造点,由blockingqueue的offer改成put阻塞方法executor.getQueue().put(r);} catch (InterruptedException e) {e.printStackTrace();}}  }  // 测试构造的线程池  public static void main(String[] args) {  CustomThreadPoolExecutor exec = new CustomThreadPoolExecutor();  // 1.初始化  exec.init();  ExecutorService pool = exec.getCustomThreadPoolExecutor();  for(int i=1; i<100; i++) {  System.out.println("提交第" + i + "个任务!");  pool.execute(new Runnable() {  @Override  public void run() {  try {  System.out.println(">>>task is running====="); TimeUnit.SECONDS.sleep(10);} catch (InterruptedException e) {  e.printStackTrace();  }  }  });  }  // 2.销毁----此处不能销毁,因为任务没有提交执行完,如果销毁线程池,任务也就无法执行了  // exec.destory();  try {  Thread.sleep(10000);  } catch (InterruptedException e) {  e.printStackTrace();  }  }
}  

解释:当提交任务被拒绝时,进入拒绝机制,我们实现拒绝方法,把任务重新用阻塞提交方法put提交,实现阻塞提交任务功能,防止队列过大,OOM,提交被拒绝方法在下面

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))// 进入拒绝机制, 我们把runnable任务拿出来,重新用阻塞操作put,来实现提交阻塞功能reject(command);}

总结: 
1、用ThreadPoolExecutor自定义线程池,看线程是的用途,如果任务量不大,可以用无界队列,如果任务量非常大,要用有界队列,防止OOM 
2、如果任务量很大,还要求每个任务都处理成功,要对提交的任务进行阻塞提交,重写拒绝机制,改为阻塞提交。保证不抛弃一个任务 
3、最大线程数一般设为2N+1最好,N是CPU核数 
4、核心线程数,看应用,如果是任务,一天跑一次,设置为0,合适,因为跑完就停掉了,如果是常用线程池,看任务量,是保留一个核心还是几个核心线程数 
5、如果要获取任务执行结果,用CompletionService,但是注意,获取任务的结果的要重新开一个线程获取,如果在主线程获取,就要等任务都提交后才获取,就会阻塞大量任务结果,队列过大OOM,所以最好异步开个线程获取结果

转载自:https://www.cnblogs.com/zedosu/p/6665306.html

转载于:https://www.cnblogs.com/PengChengLi/p/10298722.html

ThreadPoolExecutor使用详解相关推荐

  1. java线程池ThreadPoolExecutor类详解

    线程池有哪些状态 1. RUNNING:  接收新的任务,且执行等待队列中的任务 Accept new tasks and process queued tasks  2. SHUTDOWN: 不接收 ...

  2. 线程池详解(通俗易懂超级好)

    目标 [理解]线程池基本概念 [理解]线程池工作原理 [掌握]自定义线程池 [应用]java内置线程池 [应用]使用java内置线程池完成综合案例 线程池 线程池基础 线程池使用 线程池综合案例 学员 ...

  3. Java线程池详解学习:ThreadPoolExecutor

    Java线程池详解学习:ThreadPoolExecutor Java的源码下载参考这篇文章:Java源码下载和阅读(JDK1.8) - zhangpeterx的博客 在源码的目录java/util/ ...

  4. ThreadPoolExecutor详解及线程池优化

    前言 ThreadPoolExecutor在concurrent包下,是我们最常用的类之一.无论是做大数据的,还是写业务开发,对其透彻的理解以及如何发挥更好的性能,成为了我们在更好的coding道路上 ...

  5. ThreadPoolExecutor运转机制及BlockingQueue详解

    1.ThreadPoolExecutor的构建参数 最近发现几起对ThreadPoolExecutor的误用,其中包括自己,发现都是因为没有仔细看注释和内部运转机制,想当然的揣测参数导致,先看一下新建 ...

  6. 多线程之ThreadPoolExecutor详解

    一.为什么使用ThreadPoolExecutor来创建线程池 线程资源必须通过线程池提供,不允许在应用中自行显式创建线程. 因为线程池的好处是减少在创建和销毁线程上所消耗的时间以及系统资源的开销,解 ...

  7. java线程池使用详解ThreadPoolExecutor使用示例

    一 使用线程池的好处 二 Executor 框架 2.1 简介 2.2 Executor 框架结构(主要由三大部分组成) 1) 任务(Runnable /Callable) 2) 任务的执行(Exec ...

  8. 线程池ThreadPoolExecutor详解(整理详细)

    ThreadPoolExecutor 1.什么是线程池? (首先要理解什么是线程) 线程池,thread pool,是一种线程使用模式,线程池维护着多个线程,等待着监督管理者分配可并发执行的任务. 通 ...

  9. java threadpoolexecutor 返回值_Java ThreadPoolExecutor详解

    ThreadPoolExecutor是Java语言对于线程池的实现.池化技术是一种复用资源,减少开销的技术.线程是操作系统的资源,线程的创建与调度由操作系统负责,线程的创建与调度都要耗费大量的资源,其 ...

最新文章

  1. C语言打印1000以内的完数
  2. 支付宝支付 第六集:生成支付二维码
  3. 54款开源服务器软件(内容管理、数据库、电子商务、邮件服务器、文件传输、操作系统、安全、小公司服务 .
  4. leetcode 90. 子集 II 思考分析
  5. DB2常用傻瓜问题1000问(之一)
  6. [转]Android web开发快速入门
  7. 计算机考研什么时候开始备考,上岸前辈告诉你,考研数学什么时候开始复习最好?...
  8. 《白帽子讲web安全》第1章 我的安全世界观
  9. Windows:无须再忍,Microsoft Store下载慢/加速/更快,不摘抄(2022新)
  10. Apple pay 论述
  11. OSChina 周二乱弹 —— 程序员如何转行卖烧烤
  12. windows11安装日语输入法(添加输入法)
  13. 计算机专业夏令营英语面试范文,夏令营面试英文自我介绍
  14. 医院、诊所看这里,一个分诊屏+叫号系统,实现门诊高效排队叫号
  15. 达梦数据库 开发版试用时间限制
  16. 京东技术体系员工级别划分及薪资区间
  17. PS 2019 Mac版 自学入门系列(四)——调配颜色
  18. 最新精华版申请苹果开发者账号-企业版
  19. 悼念图灵奖得主、ML语言之父Robin Milner
  20. D/A转换器(DAC)

热门文章

  1. Sql Server2005 Transact-SQL 新兵器学习总结之-数据类型
  2. 用栈来表示队列,用队列来表示栈
  3. 使用Apache自带的ab命令测试网站性能(小强性能测试班学员作品)
  4. Linux使用单用户模式修改root密码.
  5. listview改变选中行字体颜色
  6. 在单块磁盘上安装2000和XP操作系统
  7. 卓瑞机器人_校企合作专业共建记涪陵职教中心机器人专业中泰学术交流活动
  8. AutoMl 的pytorch类似代码
  9. pytorch强化学习训练倒摆小车
  10. python3-泊松分布