概览

博客地址 http://blog.csdn.net/qq_25806863

原文地址 http://blog.csdn.net/qq_25806863/article/details/71159452

通过上一篇对ThreadPoolExecutor的构造方法分析可以感受到,通过ThreadPoolExecutor来创建线程池是比较复杂的,参数比较多,考虑因素也多。

因此java自己提供了一个工厂类Executors,里面提供了一些方法,用来创建常用的几种ThreadPoolExecutor线程池。

下面是方法概览:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}

分析

FixedThreadPool

FixedThreadPool调用的是ThreadPoolExecutor的构造方法。有下面两种使用方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
//不带工厂的
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);
}

通过对ThreadPoolExecutor构造参数的分析可以看出来,这个线程池的最大线程数就是核心线程数,也就是没有非核心线程的存在。这些线程都是核心线程,即使在闲置状态也不会被回收,除非线程池关闭了,所以超时机制并没有用。他的任务队列是无界的LinkedBlockingQueue,因此超过核心线程数量的任务会放在队列中排队。

这样的线程池优点很明显,只会创建固定数量的线程,然后这些线程一直存活重用,不会有额外的创建和销毁线程的开销,能更快的执行任务。

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ExecutorService executor = Executors.newFixedThreadPool(2);
Runnable myRunnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + " run time: "+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
for (int i = 0; i < 6; i++) {
executor.execute(myRunnable);
}

创建一个固定大小为2的FixedThreadPool,然后添加6个任务,输出是:

1
2
3
4
5
6
pool-1-thread-2 run time: 1493863106021
pool-1-thread-1 run time: 1493863106021
pool-1-thread-2 run time: 1493863108022
pool-1-thread-1 run time: 1493863108022
pool-1-thread-1 run time: 1493863110026
pool-1-thread-2 run time: 1493863110027

可以看到这个线程池只会创建2个线程,其他的都在排队。

线程工厂

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public static void main(String[] args) throws InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(2,new MyFactory());
Runnable myRunnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + " run time: "+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
for (int i = 0; i < 6; i++) {
executor.execute(myRunnable);
}
}
static class MyFactory implements ThreadFactory{
@Override
public Thread newThread(@NonNull Runnable r) {
Thread thread = new Thread(r);
thread.setName("哈哈"+ UUID.randomUUID().toString().substring(0,3));
return thread;
}
}

创建一个工厂,自定义设置线程名字,然后在newFixedThreadPool时传入工厂,看输出

1
2
3
4
5
6
哈哈fea run time: 1493863498244
哈哈491 run time: 1493863498244
哈哈fea run time: 1493863500250
哈哈491 run time: 1493863500250
哈哈fea run time: 1493863502253
哈哈491 run time: 1493863502253

下面的工厂参数都是这样,就不在写了。

CachedThreadPool

CachedThreadPool也是用的ThreadPoolExecutor的构造方法

1
2
3
4
5
6
7
8
9
10
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
public static ScheduledExecutorService newSingleThreadScheduledExecutor(ThreadFactory threadFactory) {
return new DelegatedScheduledExecutorService
(new ScheduledThreadPoolExecutor(1, threadFactory));
}

分析这个参数可以看出,这个线程池没有核心线程,所有的线程都会在限制60秒后被回收。而且最大线程数为Integer.MAX_VALUE,相当于无限大。因为任务队列是SynchronousQueue,不会保存任何任务,所以当有新任务时,如果当前线程都在活动着,就会新建一个线程来执行任务。

这样的线程池的特点就是,适合执行大量的耗时短的任务。而且当所有任务执行完后,闲置超过60秒就会全部回收,这是线程池里就没有任何线程,不会占用系统资源。

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
ExecutorService executor = Executors.newCachedThreadPool();
Runnable myRunnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + " run time: "+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
for (int i = 0; i < 3; i++) {
executor.execute(myRunnable);
}
Thread.sleep(30000);
System.out.println("睡眠30秒后");
for (int i = 0; i < 3; i++) {
executor.execute(myRunnable);
}
Thread.sleep(65000);
System.out.println("再睡眠65秒后");
for (int i = 0; i < 3; i++) {
executor.execute(myRunnable);
}

先运行三个任务,30秒后再添加3个任务,再过65秒再添加3个任务。

1
2
3
4
5
6
7
8
9
10
11
pool-1-thread-1 run time: 1493864065635
pool-1-thread-2 run time: 1493864065635
pool-1-thread-3 run time: 1493864065639
睡眠30秒后
pool-1-thread-3 run time: 1493864095637
pool-1-thread-2 run time: 1493864095637
pool-1-thread-1 run time: 1493864095637
再睡眠65秒后
pool-1-thread-5 run time: 1493864160643
pool-1-thread-4 run time: 1493864160643
pool-1-thread-6 run time: 1493864160643

可以看到一开始创建了3个线程,30秒后重用了这三个线程。但是再过65秒后,者三个线程就因为超时被回收了,所以新建了三个线程。

SingleThreadExecutor

SingleThreadExecutor使用new FinalizableDelegatedExecutorService来创建线程池。但是其实通过一个委托调用了ThreadPoolExecutor的构造方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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));
}

从ThreadPoolExecutor的构造参数依然可以看出,这个线程池仅有一个核心线程,其他的任务都在任务队列中排队。

这样的线程池会让所有的任务都在同一个线程中执行,避免的同步问题。

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
ExecutorService executor = Executors.newSingleThreadExecutor();
Runnable myRunnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + " run time: "+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
for (int i = 0; i < 3; i++) {
executor.execute(myRunnable);
}
Thread.sleep(65000);
System.out.println("睡眠65秒后");
for (int i = 0; i < 3; i++) {
executor.execute(myRunnable);
}
1
2
3
4
5
6
7
pool-1-thread-1 run time: 1493864476076
pool-1-thread-1 run time: 1493864478078
pool-1-thread-1 run time: 1493864480082
睡眠65秒后
pool-1-thread-1 run time: 1493864541080
pool-1-thread-1 run time: 1493864543083
pool-1-thread-1 run time: 1493864545087

自始至终都只有一个线程。而且不会被回收。

ScheduledThreadPool

ScheduledThreadPool使用new ScheduledThreadPoolExecutor来创建

1
2
3
4
5
6
7
8
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public static ScheduledExecutorService newScheduledThreadPool(
int corePoolSize, ThreadFactory threadFactory) {
return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}

看一下具体使用的构造参数:

1
2
3
4
5
6
public ScheduledThreadPoolExecutor(int corePoolSize,
ThreadFactory threadFactory) {
super(corePoolSize, Integer.MAX_VALUE,
DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
new DelayedWorkQueue(), threadFactory);
}

可以看到,这个线程池的核心线程数是固定的,但是总线程数是无限的,然而因为DelayedWorkQueue是个无界队列,所以这个值没有意义,超过核心线程的任务都会放在队列中。

ScheduledThreadPoolExecutor主要是用来执行定时任务和有周期性的重复任务。

这里就举个不恰当的例子,不是ScheduledThreadPoolExecutor的用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ExecutorService executor = Executors.newScheduledThreadPool(3);
Runnable myRunnable = new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
System.out.println(Thread.currentThread().getName() + " run time: "+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
for (int i = 0; i < 6; i++) {
executor.execute(myRunnable);
}
1
2
3
4
5
6
pool-1-thread-3 run time: 1493866372444
pool-1-thread-1 run time: 1493866372444
pool-1-thread-2 run time: 1493866372444
pool-1-thread-2 run time: 1493866374450
pool-1-thread-1 run time: 1493866374450
pool-1-thread-3 run time: 1493866374450

设置的是3,就只会创建3个线程。