1、Executors 简介
ThreadPoolExecutor 通常使用 工厂类Executors来创建,Executors可以创建3种类型的ThreadPoolExecutor:FixedThreadPool、SingleThreadExecutor、CachedThreadPool。
ThreadPoolExecutor
- 可以灵活的自定义的创建线程池,可定制性很高
- 想创建好一个合适的线程池比较难
- 使用稍微麻烦一些
- 实际中很少使用
Executors
- 可以创建4种线程池,这四种线程池基本上已经包含了所有需求,将来根据业务特点选用就好
- 使用非常简单
- 实际中很常用
1.1、FixedThreadPool
创建一个线程数固定(corePoolSize==maximumPoolSize)的线程池
核心线程会一直运行
如果一个核心线程由于异常跪了,会新创建一个线程
无界队列LinkedBlockingQueue
1.2、SingleThreadExecutor
创建一个线程数固定(corePoolSize==maximumPoolSize==1)的线程池
核心线程会一直运行
无界队列LinkedBlockingQueue
所有task都是串行执行的(即同一时刻只有一个任务在执行)
1.3、CachedThreadPool
corePoolSize==0
maximumPoolSize==Integer.MAX_VALUE
队列:SynchronousQueue
创建一个线程池:当池中的线程都处于忙碌状态时,会立即新建一个线程来处理新来的任务
这种池将会在执行许多耗时短的异步任务的时候提高程序的性能
6秒钟内没有使用的线程将会被中止,并且从线程池中移除,因此几乎不必担心耗费资源
2、Executors工厂类 的 成员变量
没有成员变量,Executors 的功能就是 new 线程池用的
public class Executors {
......
没有成员变量
}
3、重要内部类
3.1、DefaultThreadFactory
默认线程工厂
static class DefaultThreadFactory implements ThreadFactory {
// 静态成员变量,记录DefaultThreadFactory目前创建的 线程池个数
private static final AtomicInteger poolNumber = new AtomicInteger(1);
private final ThreadGroup group;
// 当前线程池线程个数
private final AtomicInteger threadNumber = new AtomicInteger(1);
private final String namePrefix;
// 构造方法
DefaultThreadFactory() {
SecurityManager s = System.getSecurityManager();
group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
namePrefix = "pool-" + poolNumber.getAndIncrement() + "-thread-";
}
public Thread newThread(Runnable r) {
// 创建一个线程
Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
// 如果是后台服务线程,就设置
if (t.isDaemon())
t.setDaemon(false);
// 设置优先级
if (t.getPriority() != Thread.NORM_PRIORITY)
t.setPriority(Thread.NORM_PRIORITY);
return t;
}
}
工厂接口 :里面只定义了1个方法:newThread(Runable r)
public interface ThreadFactory {
Thread newThread(Runnable r);
}
4、构造方法
注意:private,Executors不能生成实例!!!
private Executors() {}
5、ThreadPoolExecutor 类的线程池
5.1、newFixedThreadPool 系列
大小固定的线程池(一旦初始化完成,线程池大小就不能改变了!!!)
corePoolSize==maximumPoolSize
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory);
}
5.2、newScheduledThreadPool 系列
corePoolSize==maximumPoolSize==1
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService(
new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>(),
threadFactory));
}
5.3、CachedThreadPool 系列
corePoolSize==0
maximumPoolSize==Integer.MAX_VALUE
队列:SynchronousQueue
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory);
}
6、ForkJoinPool 类线程池
7、ScheduledThreadPoolExecutor 类线程池