Executors工具类源码分析

357 阅读1分钟

1、executors提供了4种实现线程池方式 ①、newFixedThreadPool②、newSingleThreadExecutor③、 newCachedThreadPool④、newScheduledThreadPool来进行实现 2、ThreadPoolExecutor(int corePoolSize,int maximumPoolSize,long keepAliveTime,TimeUnit unit,BlockingQueue workQueue) 核心参数描述
corePoolSize the number of threads to keep in the pool,even they are idle,unless is set maximumPoolSize 保存线程数在线程池,即将在空闲,除非当前线程=maximumPoolSize,并且workQueue对列任务存满 maximumPoolSize the maxium number of threads allow in the pool 最大线程数允许在线程池中 keepAliveTime when the number of threads is greater than the core,this is the maxium time that excess idle threads will wait for new tasks before terminating unit the time unit for the argument workQueue the queue to use for handling tasks before they are executed.this queue will hold only the tasks submitted by the method. 3、newFixedThreadPool可以创建一个可重用的固定线程的线程池,以共享的无界队列方式来运行这些线程

4、newSingleThreadExecutor 创建一个单例工作,一直是单个线程处于工作。

5、newCachedThreadPool 缓存线程池

6、newScheduledThreadPool 定时任务线程池