核心要点

ThreadPoolExecutor的有参构造-LMLPHP

参数最多的构造方法展示

  • 映入眼帘的是一些健壮性校验
  • corePoolSize < 0;可以发现这个判断是核心线程的个数不能小于零。但是也就说明核心线程的个数可以等于零。
  • maximumPoolSize <= 0 || maximumPoolSize < corePoolSize ;这个判断说明最大的线程数要大于零,并且最大线程数要大于核心线程数
  • keepAliveTime < 0。非核心线程空闲时间大于等于零都可以。
  • throw new IllegalArgumentException()。要是上述的条件不满足那就是抛异常:IllegalArgumentException().非法参数异常。
  • 分析下面有一个if判断:
  • workQueue == null || threadFactory == null || handler == null。阻塞队列、线程工厂、拒绝策略都不能为null。要是条件不满足那就throw new NullPointerException();抛出空指针异常。
  • this.acc = System.getSecurityManager() == null ? null : AccessController.getContext();这是一个系统资源访问策略。目前看不懂。。。。。。
  • this.corePoolSize = corePoolSize;
    this.maximumPoolSize = maximumPoolSize;
    this.workQueue = workQueue;
    this.keepAliveTime = unit.toNanos(keepAliveTime);
    this.threadFactory = threadFactory;
    this.handler = handler;

  • 上述这六个是将全局变量赋值为局部变量,是并发编程大师到隔离的编程习惯。

  • 涉及到时间的单位是纳秒。

ThreadPoolExecutor的有参构造-LMLPHP

06-26 04:16