线程池第二篇:线程池相关类

一、线程池类ThreadPoolExecutor

ThreadPoolExecutor实现了ExecutorService接口,是线程池的代表类。

构造器最多有7个参数,各参数意思如下:

 

二、ForkJoinPool

ForkJoinPool也实现了ExecutorService接口,参考https://www.cnblogs.com/koushr/p/12044576.html

 

三、ScheduledThreadPoolExecutor

ScheduledThreadPoolExecutor是ThreadPoolExecutor的子类,继承了ScheduledExecutorService接口。ScheduledExecutorService接口是ExecutorService的子接口。

四、线程池工具类Executors

Executors的很多静态方法都会生成线程池实例。

1、ExecutorService static newFixedThreadPool(int nThreads)

核心线程数和最大线程数一样,除非线程池被关闭,否则线程不会被销毁。阻塞队列用的是LinkedBlockingQueue的无界模式。

2、ExecutorService static newSingleThreadExecutor()

等价于newFixedThreadPool(1)。

3、ExecutorService static newCachedThreadPool()

核心线程数是0,最大线程数是Integer.MAX_VALUE,创建的线程会复用,在已有线程来不及复用的情况下,来一个任务就启动一个线程,有OOM风险。线程空闲超过60s就会被销毁。阻塞队列用的是SynchronousQueue。

4、ExecutorService static newWorkStrealingPool()、ExecutorService static newWorkStealingPool(int parallelism)

 

两个特殊的方法,生成定时任务线程池实例。

5、ScheduledExecutorService static newScheduledThreadPool(int corePoolSize)

核心线程数是corePoolSize,最大线程数是Integer.MAX_VALUE,线程空闲超过10ms就会被销毁。阻塞队列用的是ScheduledThreadPoolExecutor.DelayedWorkQueue。

6、ScheduledExecutorService static newSingleThreadScheduledExecutor()

等价于newScheduledThreadPool(1)。

posted on 2019-10-20 01:43  koushr  阅读(170)  评论(0编辑  收藏  举报

导航