博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java并发相关(4)---线程池ThreadPoolExecutor
阅读量:4075 次
发布时间:2019-05-25

本文共 3383 字,大约阅读时间需要 11 分钟。

 *    ThreadPoolExecutor(线程池)

 *    理论:
 *        1、概念:线程池做的工作主要是控制运行的线程的数量,处理过程中将任务加入队列,然后在线程创建后启动这些任务。
 *        2、运作流程:当前线程数 < 核心线程,创建新线程,当前线程数 > 核心 < 最大,加入队列,当前队列满,创建新线程。
 *        超过最大线程并且队列满则抛弃。

一、ThreadPoolExecutor的参数解释:

 * 1、核心参数:

     *         corePoolSize: 常驻核心线程数
     *         maximumPoolSize: 最大线程数
     *         workQueue: 任务等待队列
     *             常见任务队列:
     *                 1)  SynchronousQueue (直接提交)
     *                 2)  LinkedBlockingQueue (无参 --- 无界队列)
     *                 3)  ArrayBlockingQueue (有界队列)
     * 2、其他参数
     *         keepAliveTime:线程空闲多少时间销毁,即大于core线程时,多余的线程空闲时销毁,线程池数量降至core线程数量。
     *         unit: 时间单位。
     *         RejectedExecutionHandler:任务等待队列满了以后的拒绝策略
     *             AbortPolicy (默认): 抛出RejectedException异常
     *             CallerRunPolicy:不丢弃,但是采用一些策略减缓新任务的提交速度
     *             DiscardOldestPolicy:丢弃等待最长时间的
     *             DiscardPolicy:丢弃任务

public void base1() {//		public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime,//                   TimeUnit unit, BlockingQueue
workQueue, RejectedExecutionHandler handler) new ThreadPoolExecutor(testcorePoolSize, testcorePoolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue
()); }

     *    二、通过Executors创建线程池的常用方法

     *  3) newSingleThreadExecutor  创建1个核心线程和max线程,任务队列为无界队列。

     *  2) newFixedThreadPool    创建指定个数core线程和max线程,任务队列为无界队列。
     *  1) newCachedThreadPool    创建0个核心线程,最大线程Integer.max,队列为SynchronousQueue。
     *  4) newScheduledThreadPool 创建指定个数核心线程,最大线程,任务队列DelayedWorkQueue。
     *  上面构造存在问题:newScheduledThreadPool和newCachedThreadPool指定了最大线程数为MAX_VALUE,可能会创建大量的线程,从而导致OOM。
     *                   newFixedThreadPool和newSingleThreadExecutor的队列是无界的,可能堆积大量任务,从而导致OOM。

public void base2() {		ExecutorService t1 = Executors.newCachedThreadPool();		new ThreadPoolExecutor(0, Integer.MAX_VALUE,  60L, TimeUnit.SECONDS, new SynchronousQueue
()); ExecutorService t2 = Executors.newFixedThreadPool(testcorePoolSize); new ThreadPoolExecutor(testcorePoolSize, testcorePoolSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue
()); ExecutorService t3 = Executors.newSingleThreadExecutor(); new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue
()); ScheduledExecutorService t4 = Executors.newScheduledThreadPool(testcorePoolSize);// super(testcorePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS, new DelayedWorkQueue(); }

     * 三、使用案例

     *     1、newCachedThreadPool 会启动20个线程进行执行。
     *  2、newFixedThreadPool 会启动10个线程执行,另外10个任务先加入等待队列,10个任务执行完,再执行下10个,线程数一直是10个。
     *  3、newSingleThreadExecutor 会启动1个线程执行, 另外任务加入等待队列。
     *  4、自定义: 见下面注释。

public static void main(String[] args) {        //ExecutorService threadPool = Executors.newCachedThreadPool();		// ExecutorService threadPool = Executors.newFixedThreadPool(10);		// ExecutorService threadPool = Executors.newSingleThreadExecutor();				// 自定义线程池解释:core 线程2个,max线程5个,队列3个,		// 预期流程是:当启动 线程少于core线程,启动core2个,当等于core少于max,加入队列,队列装了3个,装不下,		// 再启动3个线程至max数量,此时5个线程执行任务,3个任务等待,其他的由于拒绝策略是抛弃,因此执行了8个任务。        ExecutorService threadPool = new ThreadPoolExecutor(            2, 5, 1L, TimeUnit.SECONDS,            new LinkedBlockingDeque
(3), Executors.defaultThreadFactory(), new ThreadPoolExecutor.DiscardOldestPolicy() ); try { for (int i = 1; i <= 20; i++) { threadPool.execute(() -> { try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + "\t 来办理业务"); }); } } catch (Exception e) { e.printStackTrace(); } finally { threadPool.shutdown(); } }

 

转载地址:http://keuni.baihongyu.com/

你可能感兴趣的文章
uSurvival 1.41多人在线生存逃杀吃鸡类游戏源码
查看>>
玩转@Git三剑客
查看>>
Unity实现c#热更新方案探究(一)
查看>>
uSurvival 1.41多人在线生存逃杀吃鸡类游戏源码
查看>>
AXURE RP8 - 实战手册 网站和APP原型制作案例精粹
查看>>
c#传不确定的参数个数,比如int型
查看>>
NGUI: Next-Gen UI 2018.3.0f
查看>>
玩转@Git三剑客
查看>>
Android 9.0 Http不能访问网络
查看>>
Android 9.0 Http不能访问网络
查看>>
Unity编辑器环境在Inspector面板中显示变量
查看>>
苹果IPhone真机开发调试
查看>>
UE4虚幻引擎独立游戏制作教程 UE4编程教学 虚幻引擎4
查看>>
revenue
查看>>
currency
查看>>
iTunes Connect上传APP屏幕快照图片失败 - 您必须上传有效的屏幕快照。
查看>>
receipt
查看>>
[教程] Packt - Create a Game Environment with Blender and Unity by Darrin Lile
查看>>
[教程] Packt - Create a Game Environment with Blender and Unity by Darrin Lile
查看>>
详解Unity Profiler内存分析问题
查看>>