1
2
3
4
5
6
7
graph TB
e(Executor<V>)-->es(ExecutorService)
es-->se(ScheduledExecutorService)
es-->aes(AbstractExecutorService)
aes-->tpe(ThreadPoolExecutor)
tpe-->ste(ScheduledThreadPoolExecutor)
se-->ste
ExecutorService
一个运行新任务的简单接口。
ExecutorService,扩展了Executor接口。添加了一些用来管理执行器生命周期和任务生命周期的方法。
可以通过java.util.concurrent.Executors
类,提供了多个实例化线程的简易静态工厂方法,来创造ExecutorService的子类(一般是创建ThreadPoolExecutor/ScheduledExecutorService/ScheduledThreadPoolExecutor)
ScheduledExecutorService
继承了ExecutorService和Executor。支持Future和定期执行任务。
AbstractExecutorService
ThreadPoolExecutor
实现了ExecutorService,所以才能够这么玩
1
2
3
4
5
6
7
//guava
ThreadFactory factory = new ThreadFactoryBuilder().setNameFormat("ExecutorServiceExample-%d").build();
ExecutorService executor = new ThreadPoolExecutor(1,
200,
0L,
TimeUnit.DAYS,
new LinkedBlockingDeque<Runnable>(1024),factory);
ScheduledThreadPoolExecutor
从上图的继承树就可以看出,ScheduledThreadPoolExecutor通过继承ScheduledExecutorService
接口实现了其特性,多出了一个awaitTermination
方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Runnable runnabledelayedTask = new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + " is Running Delayed Task");
}
};
Callable callabledelayedTask = new Callable() {
@Override
public String call() throws Exception {
return "GoodBye! See you at another invocation...";
}
};
ScheduledExecutorService scheduledPool = Executors.newScheduledThreadPool(4);
scheduledPool.scheduleWithFixedDelay(runnabledelayedTask, 1, 1, TimeUnit.SECONDS);
ScheduledFuture sf = scheduledPool.schedule(callabledelayedTask, 4, TimeUnit.SECONDS);
String value = (String) sf.get();
System.out.println("Callable returned" + value);
scheduledPool.shutdown();
System.out.println("Is ScheduledThreadPool shutting down? " + scheduledPool.isShutdown());