在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

ThreadPoolExecutor的参数理解

2017-2-9 13:05| 发布者: zhangjf| 查看: 584| 评论: 0

摘要: 一、使用Executors创建线程池 之前创建线程的时候都是用的Executors的newFixedThreadPool(),newSingleThreadExecutor(),newCachedThreadPool()这三个方法。 1. newFixedThreadPool() 创建线 ...
一、使用Executors创建线程池

之前创建线程的时候都是用的Executors的newFixedThreadPool(),newSingleThreadExecutor(),newCachedThreadPool()这三个方法。

1. newFixedThreadPool()

创建线程数固定大小的线程池。

2.newSingleThreadPool()

创建线程数为1的线程池,

3.newCachedThreadPool()

创建可缓冲的线程池。没有大小限制


二、使用ThreadPoolExecutor创建线程池 ThreadPoolExecutor的构造函数
  1. public ThreadPoolExecutor(int corePoolSize,
  2. int maximumPoolSize,
  3. long keepAliveTime,
  4. TimeUnit unit,
  5. BlockingQueue<Runnable> workQueue,
  6. ThreadFactory threadFactory,
  7. RejectedExecutionHandler handler) {
  8. if (corePoolSize < 0 ||
  9. maximumPoolSize <= 0 ||
  10. maximumPoolSize < corePoolSize ||
  11. keepAliveTime < 0)
  12. throw new IllegalArgumentException();
  13. if (workQueue == null || threadFactory == null || handler == null)
  14. throw new NullPointerException();
  15. this.corePoolSize = corePoolSize;
  16. this.maximumPoolSize = maximumPoolSize;
  17. this.workQueue = workQueue;
  18. this.keepAliveTime = unit.toNanos(keepAliveTime);
  19. this.threadFactory = threadFactory;
  20. this.handler = handler;
  21. }
复制代码
参数:

1、corePoolSize核心线程数大小,当线程数

2、maximumPoolSize 最大线程数, 当线程数 >= corePoolSize的时候,会把runnable放入workQueue中

3、keepAliveTime 保持存活时间,当线程数大于corePoolSize的空闲线程能保持的最大时间。

4、unit 时间单位

5、workQueue 保存任务的阻塞队列

6、threadFactory 创建线程的工厂

7、handler 拒绝策略

任务执行顺序:

1、当线程数小于corePoolSize时,创建线程执行任务。

2、当线程数大于等于corePoolSize并且workQueue没有满时,放入workQueue中

3、线程数大于等于corePoolSize并且当workQueue满时,新任务新建线程运行,线程总数要小于maximumPoolSize

4、当线程总数等于maximumPoolSize并且workQueue满了的时候执行handler的rejectedExecution。也就是拒绝策略。

ThreadPoolExecutor默认有四个拒绝策略:

1、ThreadPoolExecutor.AbortPolicy() 直接抛出异常RejectedExecutionException

2、ThreadPoolExecutor.CallerRunsPolicy() 直接调用run方法并且阻塞执行

3、ThreadPoolExecutor.DiscardPolicy() 直接丢弃后来的任务

4、ThreadPoolExecutor.DiscardOldestPolicy() 丢弃在队列中队首的任务

当然可以自己继承RejectedExecutionHandler来写拒绝策略.

  1. int corePoolSize = 1;
  2. int maximumPoolSize = 2;
  3. int keepAliveTime = 10;
  4. // BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<Runnable>();
  5. BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(5);
  6. ThreadFactory threadFactory = Executors.defaultThreadFactory();
  7. //线程池和队列满了之后的处理方式
  8. //1.跑出异常
  9. RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
  10. RejectedExecutionHandler handler2 = new ThreadPoolExecutor.CallerRunsPolicy();
  11. RejectedExecutionHandler handler3 = new ThreadPoolExecutor.DiscardPolicy();
  12. RejectedExecutionHandler handler4 = new ThreadPoolExecutor.DiscardOldestPolicy();
  13. ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, workQueue, threadFactory, handler2);
  14. for (int j = 1; j < 15; j++) {
  15. threadPoolExecutor.execute(new Runnable() {
  16. public void run() {
  17. try {
  18. System.out.println(Thread.currentThread().getName());
  19. TimeUnit.SECONDS.sleep(1);
  20. } catch (InterruptedException e) {
  21. e.printStackTrace();
  22. }
  23. }
  24. });
  25. }
  26. System.out.println(threadPoolExecutor);
  27. }
复制代码


来自:http://my.oschina.net/u/2250599/blog/498787

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-9 22:00

Copyright 2015-2025 djqfx

返回顶部