JAVA自定义线程池

雨点打透心脏的1/2处 2021-11-13 14:48 482阅读 0赞

自定义线程池ThreadPoolExecutor的构造函数有7个参数

源码

  1. /** * Creates a new {@code ThreadPoolExecutor} with the given initial * parameters. * * @param corePoolSize the number of threads to keep in the pool, even * if they are idle, unless {@code allowCoreThreadTimeOut} is set * @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 {@code keepAliveTime} argument * @param workQueue the queue to use for holding tasks before they are * executed. This queue will hold only the {@code Runnable} * tasks submitted by the {@code execute} 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 * @throws IllegalArgumentException if one of the following holds:<br> * {@code corePoolSize < 0}<br> * {@code keepAliveTime < 0}<br> * {@code maximumPoolSize <= 0}<br> * {@code maximumPoolSize < corePoolSize} * @throws NullPointerException if {@code workQueue} * or {@code threadFactory} or {@code handler} is null */
  2. public ThreadPoolExecutor(int corePoolSize,
  3. int maximumPoolSize,
  4. long keepAliveTime,
  5. TimeUnit unit,
  6. BlockingQueue<Runnable> workQueue,
  7. ThreadFactory threadFactory,
  8. RejectedExecutionHandler handler) {
  9. if (corePoolSize < 0 ||
  10. maximumPoolSize <= 0 ||
  11. maximumPoolSize < corePoolSize ||
  12. keepAliveTime < 0)
  13. throw new IllegalArgumentException();
  14. if (workQueue == null || threadFactory == null || handler == null)
  15. throw new NullPointerException();
  16. this.acc = System.getSecurityManager() == null ?
  17. null :
  18. AccessController.getContext();
  19. this.corePoolSize = corePoolSize;
  20. this.maximumPoolSize = maximumPoolSize;
  21. this.workQueue = workQueue;
  22. this.keepAliveTime = unit.toNanos(keepAliveTime);
  23. this.threadFactory = threadFactory;
  24. this.handler = handler;
  25. }

参数含义

  • corePoolSize- 池中所保存的线程数,包括空闲线程。需要注意的是在初创建线程池时线程不会立即启动,直到有任务提交才开始启动线程并逐渐使线程数目达到corePoolSize。若想一开始就创建所有核心线程需调用prestartAllCoreThreads方法。
  • maximumPoolSize-池中允许的最大线程数。需要注意的是当核心线程满且阻塞队列也满时才会判断当前线程数是否小于最大线程数,并决定是否创建新线程。
  • keepAliveTime - 当线程数大于核心时,多于的空闲线程最多存活时间
  • unit - keepAliveTime 参数的时间单位。
  • workQueue - 当线程数目超过核心线程数时用于保存任务的队列。主要有3种类型的BlockingQueue可供选择:无界队列,有界队列和同步移交。将在下文中详细阐述。从参数中可以看到,此队列仅保存实现Runnable接口的任务。
  • threadFactory - 执行程序创建新线程时使用的工厂。
  • handler - 阻塞队列已满且线程数达到最大值时所采取的饱和策略。java默认提供了4种饱和策略的实现方式:中止、抛弃、抛弃最旧的、调用者运行。

执行策略

如果运行的线程少于corePoolSize,则 Executor始终首选添加新的线程,而不进行排队。(如果当前运行的线程小于corePoolSize,则任务根本不会存入queue中,而是直接运行)
如果运行的线程大于等于 corePoolSize,则 Executor始终首选将请求加入队列,而不添加新的线程。
如果无法将请求加入队列,则创建新的线程,除非创建此线程超出 maximumPoolSize,在这种情况下,任务将被拒绝。

思维导图

在这里插入图片描述

三种阻塞队列

  1. BlockingQueue<Runnable> workQueue = null;
  2. workQueue = new ArrayBlockingQueue<>(5);//基于数组的先进先出队列,bounded(有界)
  3. workQueue = new LinkedBlockingQueue<>();//基于链表的先进先出队列,optionally-bounded( 可选有界 )
  4. workQueue = new SynchronousQueue<>();//无缓冲的等待队列,无界

任务拒接策略

  1. RejectedExecutionHandler rejected = null;
  2. rejected = new ThreadPoolExecutor.AbortPolicy();//中止策略,默认,队列满了丢任务抛出异常
  3. rejected = new ThreadPoolExecutor.DiscardPolicy();//抛弃策略,抛弃策略队列满了丢任务不抛出异常
  4. rejected = new ThreadPoolExecutor.DiscardOldestPolicy();//抛弃旧任务策略,将最早进入队列的任务删,之后再尝试加入队列
  5. rejected = new ThreadPoolExecutor.CallerRunsPolicy();//调用者运行策略,如果添加到线程池失败,那么主线程会自己去执行该任务,主线程无法再提交新任务

类图

在这里插入图片描述


  1. https://blog.csdn.net/xxj\_jing/article/details/84835476
  2. https://blog.csdn.net/u011479540/article/details/51867886

发表评论

表情:
评论列表 (有 0 条评论,482人围观)

还没有评论,来说两句吧...

相关阅读

    相关 定义java 线

     为什么要建立线程池? 在多线程项目中,如果建立的线程过多,反而可能导致运行速度大大减慢,这是由于线程建立所花费的时间和资源都比较多。 所以我们在多线程中必须很好地

    相关 定义线

    如果当前线程池中的线程数目小于corePoolSize,则每来一个任务,就会创建一个线程去执行这个任务; 如果当前线程池中的线程数目>=corePoolSize,则每来一个