java多线程之ThreadPoolExecutor
类 简介
java.uitl..类是线程池中最核心的一个类,因此如果要透彻地了解Java中的线程池,必须先了解这个类。
的4个构造方法
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), defaultHandler); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, RejectedExecutionHandler handler) { this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, Executors.defaultThreadFactory(), handler); } public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == null) throw new NullPointerException(); this.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler; }
注意: 几个参数的大小范围, >= 0, >= 1 , >= 0(为0时,表示线程永久存活java executor,即使空闲很长时间,也不会撤销)
线程池配置的各种参数以及策略
1. (核心池的大小)与 (线程池最大线程数)
2. 线程创建的时机
3. 线程创建的工厂方法
4. 保持活动时间
5. 任务队列

6. 被拒绝的任务的处理策略
当 已经关闭,并且 将有限边界用于最大线程和工作队列容量,且已经饱和时,在方法 (java.lang.) 中提交的新任务将被拒绝。在以上两种情况下,方法都将调用其 dler 的 dler.(java.lang., java.util..) 方法。下面提供了四种预定义的处理程序策略:
7. 队列维护
8. 钩子(hook)方法
此类提供 可重写的 () 和 (, ) 方法java executor,这两种方法分别在执行每个任务之前和之后调用。它们可用于操纵执行环境;例如,重新初始化 、搜集统计信息或添加日志条目。此外,还可以重写方法 () 来执行 完全终止后需要完成的所有特殊处理。

中提供了三种常用的的创建:
1. 固定线程池
固定线程池 的线程数量是固定的,由传入的参数决定。线程 为0java executorjava多线程之ThreadPoolExecutor,即不会因为空闲超时而关闭线程,同时队列是无边界的队列,不会发生任务丢弃。
public static ExecutorService newFixedThreadPool(int nThreads) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue()); }
2. tor 单线程池

单线程池中线程数量固定为1.
public static ExecutorService newSingleThreadExecutor() { return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue())); }
3. 缓存线程池
缓存线程池的核心线程 数量为0,但是池中的最大线程数是 无边界。空闲超时为60s,队列用了java多线程之ThreadPoolExecutor,即任务是立即交付运行的。
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue()); }
参考:框架(三)线程池详细介绍与
线程池-线程池的好处
1. 本站所有资源来源于用户上传和网络,如有侵权请联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系站长处理!
6. 本站不售卖代码,资源标价只是站长收集整理的辛苦费!如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。
7. 站长QQ号码 2205675299
资源库 - 资源分享下载网 » java多线程之ThreadPoolExecutor
常见问题FAQ
- 关于资源售价和售后服务的说明?
- 代码有没有售后服务和技术支持?
- 有没有搭建服务?
- 链接地址失效了怎么办?
- 关于解压密码

