源码剖析ThreadPoolExecutor线程池及阻塞队列

  本文章对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.acc = System.getSecurityManager() == null ? 16 null : 17 AccessController.getContext(); 18 //核心线程 19 this.corePoolSize = corePoolSize; 20 //最大线程数 21 this.maximumPoolSize = maximumPoolSize; 22 //阻塞队列,即今天主题 23 this.workQueue = workQueue; 24 //超时时间 25 this.keepAliveTime = unit.toNanos(keepAliveTime); 26 this.threadFactory = threadFactory; 27 //拒绝策略 28 this.handler = handler; 29 }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wpdpjy.html