java的线程池:
https://blog.youkuaiyun.com/pozmckaoddb/article/details/51478017
https://www.cnblogs.com/lic309/p/4564507.html
https://www.jianshu.com/p/f030aa5d7a28
https://blog.youkuaiyun.com/qq_30413781/article/details/79892331
newCachedThreadPool创建一个可缓存线程池,如果线程池长度超过处理需要,可灵活回收空闲线程,若无可回收,则新建线程。使用场景:快速处理大量耗时较短的任务
newFixedThreadPool 创建一个定长线程池(核心线程等于最大线程数)超出的线程会在队列中等待。 适用场景:削峰
newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
newSingleThreadExecutor 创建一个单线程化的线程池,它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。
以上四种预定义线程池使用Executors类调用,且都是通过对ThreadPoolExecutor类设定不同的参数实现的.
(如:ExecutorService cachedThreadPool = Executors.newCachedThreadPool())
参数:
ThreadPoolExecutor(
- int corePoolSize, 核心线程数
- int maximumPoolSize, 最大线程数
- long keepAliveTime, 线程空闲多久后自动结束
- TimeUnit unit, 线程最大的存活时间的单位
- BlockingQueue workQueue, 队列
- ThreadFactory threadFactory, 线程工厂
- RejectedExecutionHandler handler) 拒绝策略,当队列已满,且池中的线程数达到maximumPoolSize时,线程池拒绝添加新任务时采取的策略.DiscardPolicy:抛弃当前任务,DiscardOldestPolicy:扔掉最旧的,CallerRunsPolicy:由向线程池提交任务的线程来执行该任务,AbortPolicy:抛出 RejectedExecutionException 异常。