什么是池化技术?简单来讲就是优化资源的使用,我准备好了一些资源,有人要用就到我这里拿,用完了就还给我。而一个比较重要的的实现就是线程池。那么线程池用到了池化技术有什么好处呢?java
也就是 线程复用、能够控制最大并发数、管理线程数组
其实线程池我更愿意说成四种封装实现方式,一种原始实现方式。这四种封装的实现方式都是依赖于最原始的的实现方式。因此这里咱们先介绍四种封装的实现方式并发
这个线程池颇有意思,说是线程池,可是池子里面只有一条线程。若是线程由于异常而中止,会自动新建一个线程补充。
咱们能够测试一下:
咱们对线程池执行十条打印任务,能够发现它们用的都是同一条线程less
public static void test01() { ExecutorService threadPool = Executors.newSingleThreadExecutor(); try { //对线程进行执行十条打印任务 for(int i = 1; i <= 10; i++){ threadPool.execute(()->{ System.out.println(Thread.currentThread().getName()+"=>执行完毕!"); }); } } catch (Exception e) { e.printStackTrace(); } finally { //用完线程池必定要记得关闭 threadPool.shutdown(); } }
pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕!
这个线程池是能够指定咱们的线程池大小的,能够针对咱们具体的业务和状况来分配大小。它是建立一个核心线程数跟最大线程数相同的线程池,所以池中的线程数量既不会增长也不会变少,若是有空闲线程任务就会被执行,若是没有就放入任务队列,等待空闲线程。
咱们一样来测试一下:性能
public static void test02() { ExecutorService threadPool = Executors.newFixedThreadPool(5); try { //对线程进行执行十条打印任务 for(int i = 1; i <= 10; i++){ threadPool.execute(()->{ System.out.println(Thread.currentThread().getName()+"=>执行完毕!"); }); } } catch (Exception e) { e.printStackTrace(); } finally { //用完线程池必定要记得关闭 threadPool.shutdown(); } }
咱们建立了五条线程的线程池,在打印任务的时候,能够发现线程都有进行工做测试
pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-2=>执行完毕! pool-1-thread-3=>执行完毕! pool-1-thread-5=>执行完毕! pool-1-thread-4=>执行完毕!
这个线程池是建立一个核心线程数为0,最大线程为Inter.MAX_VALUE的线程池,也就是说没有限制,线程池中的线程数量不肯定,但若是有空闲线程能够复用,则优先使用,若是没有空闲线程,则建立新线程处理任务,处理完放入线程池。
咱们一样来测试一下
优化
建立一个没有最大线程数限制的能够定时执行线程池
在这里,还有建立一个只有单个线程的能够定时执行线程池(Executors.newSingleThreadScheduledExecutor())这些都是上面的线程池扩展开来了,不详细介绍了。this
上面咱们也说到了线程池有五种实现方式,可是实际上咱们就介绍了四种。那么最后一种是什么呢?不急,咱们能够点开咱们上面线程池实现方式的源码进行查看,能够发现线程
而点开其余几个线程池到最后均可以发现,他们实际上用的就是这个ThreadPoolExecutor
。咱们把源代码粘过来分析,其实也就是这七大参数指针
/** * 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 */ public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) { if (corePoolSize < 0 || maximumPoolSize <= 0 || maximumPoolSize < corePoolSize || keepAliveTime < 0) throw new IllegalArgumentException(); if (workQueue == null || threadFactory == null || handler == 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
。而关于这七大参数咱们简单归纳就是
Runtime.getRuntime().availableProcessors();
获取,而后设置。这里咱们用一个例子能够更好理解这些参数在线程池里面的位置和做用。
如图1.0,咱们这是一个银行
咱们一共有五个柜台,能够理解为线程池的最大线程数量,而其中有两个是在营业中,能够理解为线程池核心线程个数。而下面的等待厅能够理解为用于保存等待执行任务的阻塞队列。银行就是建立线程的工厂。
而关于空闲线程存活时间,咱们能够理解为如图1.1这种状况,当五个营业中,却只有两我的须要被服务,而其余三我的一直处于等待的状况下,等了一个小时了,他们被通知下班了。这一个小时时间就能够说是空闲线程存活时间,而存活时间单位,顾名思义。
到如今咱们就剩一个拒绝策略还没介绍,什么是拒绝策略呢?咱们能够假设当银行五个柜台都有人在被服务,如图1.2。而等待厅这个时候也是充满了人,银行实在容不下人了。
这个时候对银行外面那个等待的人的处理策略就是拒绝策略。
咱们一样了解以后用代码来测试一下:
public static void test05(){ ExecutorService threadPool = new ThreadPoolExecutor( //核心线程数量 2, //最大线程数量 5, //空闲线程存活时间 3, //存活单位 TimeUnit.SECONDS, //这里咱们使用大多数线程池都默认使用的阻塞队列,并使容量为3 new LinkedBlockingDeque<>(3), Executors.defaultThreadFactory(), //咱们使用默认的线程池都默认用的拒绝策略 new ThreadPoolExecutor.AbortPolicy() ); try { //对线程进行执行十条打印任务 for(int i = 1; i <= 2; i++){ threadPool.execute(()->{ System.out.println(Thread.currentThread().getName()+"=>执行完毕!"); }); } } catch (Exception e) { e.printStackTrace(); } finally { //用完线程池必定要记得关闭 threadPool.shutdown(); } }
咱们执行打印两条任务,能够发现线程池只用到了咱们的核心两条线程,至关于只有两我的须要被服务,因此咱们就开了两个柜台。
pool-1-thread-1=>执行完毕! pool-1-thread-2=>执行完毕!
可是在咱们将打印任务改到大于5的时候,(咱们改为8)咱们能够发现线程池的五条线程都在使用了,人太多了,咱们的银行须要都开放了来服务。
for(int i = 1; i <= 8; i++)
pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-2=>执行完毕! pool-1-thread-3=>执行完毕! pool-1-thread-4=>执行完毕! pool-1-thread-5=>执行完毕!
在咱们改为大于8的时候,能够发现拒绝策略触发了。银行实在容纳不下了,因此咱们把外面那我的用策略打发了。
for(int i = 1; i <= 9; i++)
在这里咱们也能够得出一个结论:
线程池大小= 最大线程数 + 阻塞队列大小
在上面咱们在使用的阻塞队列是大多数的线程池都使用的阻塞队列,因此就引起思考下面这个问题。
咱们在使用ThreadPoolExecutor的时候是能够本身选择拒绝策略的,而拒绝策略咱们所知道的有四种。
咱们在上面使用的就是AbortPolicy拒绝策略,在执行打印任务超出线程池大小的时候,抛出了异常。
咱们将拒绝策略修改成CallerRunsPolicy,执行后能够发现,由于第九个打印任务被拒绝了,因此它被调用者所在的线程执行了,也就是咱们的main线程。(由于它从main线程来的,如今又回到了main线程。因此咱们说它从哪里来回哪里去)
ExecutorService threadPool = new ThreadPoolExecutor( //核心线程数量 2, //最大线程数量 5, //空闲线程存活时间 3, //存活单位 TimeUnit.SECONDS, //这里咱们使用大多数线程池都默认使用的阻塞队列,并使容量为3 new LinkedBlockingDeque<>(3), Executors.defaultThreadFactory(), //咱们使用默认的线程池都默认用的拒绝策略 new ThreadPoolExecutor.CallerRunsPolicy() );
pool-1-thread-2=>执行完毕! main=>执行完毕! pool-1-thread-2=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-2=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-3=>执行完毕! pool-1-thread-4=>执行完毕! pool-1-thread-5=>执行完毕!
尝试去竞争第一个任务,可是失败了。这里就没显示了,也不抛出异常。
pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-2=>执行完毕! pool-1-thread-3=>执行完毕! pool-1-thread-4=>执行完毕! pool-1-thread-5=>执行完毕!
多出来的任务,默默抛弃掉,也不抛出异常。
pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-1=>执行完毕! pool-1-thread-2=>执行完毕! pool-1-thread-3=>执行完毕! pool-1-thread-4=>执行完毕! pool-1-thread-5=>执行完毕!
能够看到咱们的DiscardOldestPolicy与DiscardPolicy同样的结果,可是它们实际上是不同,正如咱们最开始总结的那样,DiscardOldestPolicy在多出的打印任务的时候会尝试去竞争,而不是直接抛弃掉,可是很显然竞争失败否则也不会和DiscardPolicy同样的执行结果。可是若是在线程比较多的时候就能够很看出来。