这篇文章将为大家详细讲解有关java中有哪些常见的线程池,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

首先我们列出Java 中的六种线程池如下
| 线程池名称 | 描述 |
|---|---|
| FixedThreadPool | 核心线程数与较大线程数相同 |
| SingleThreadExecutor | 一个线程的线程池 |
| CachedThreadPool | 核心线程为0,较大线程数为Integer. MAX_VALUE |
| ScheduledThreadPool | 指定核心线程数的定时线程池 |
| SingleThreadScheduledExecutor | 单例的定时线程池 |
| ForkJoinPool | JDK 7 新加入的一种线程池 |
在了解集中线程池时我们先来熟悉一下主要几个类的关系, ThreadPoolExecutor 的类图,以及 Executors 的主要方法:


上面看到的类图,方便帮助下面的理解和查看,我们可以看到一个核心类 ExecutorService , 这是我们线程池都实现的基类,我们接下来说的都是它的实现类。
FixedThreadPool
FixedThreadPool 线程池的特点是它的核心线程数和较大线程数一样,我们可以看它的实现代码在 Executors#newFixedThreadPool(int) 中,如下:
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue());
}