[TOC]
聲明
該系列文章只是記錄本人回顧java多線程編程時(shí)候記錄的筆記。文中所用語言并非嚴(yán)謹(jǐn)?shù)膶I(yè)術(shù)語(太嚴(yán)謹(jǐn)?shù)男g(shù)語其實(shí)本人也不會(huì)……)。難免有理解偏差的地方,歡迎指正。
另外,大神請繞路。不喜勿噴。
畢竟好記性不如爛筆頭嘛,而且許多東西只要不是你經(jīng)常用的最終都會(huì)一丟丟一丟丟地給忘記。
1 Excutor家族概覽
上一篇 http://blog.csdn.net/hylexus/article/details/53456463 中介紹了java線程池的相關(guān)概念和基本用法。
然而在平時(shí)真正使用的時(shí)候往往不是自己手動(dòng)創(chuàng)建線程池的。而是通過本節(jié)要介紹的Excutor和ExcutorService來使用的。
以下是Excutor家族的主要成員
2 成員介紹
2.1 Executors
Executors是一個(gè)專門為Executor、ExecutorService、ScheduledExecutorService、ThreadFactory、Callable等設(shè)計(jì)的工廠類。
2.2 ThreadPoolExecutor
ThreadPoolExecutor就是 上一篇文章 介紹的線程池。他是整個(gè)線程池的核心組件。
然而,在多數(shù)情況下,ThreadPoolExecutor的實(shí)例的創(chuàng)建并不是我們自己new的。
而是通過java.util.concurrent.Executors這個(gè)工廠類來創(chuàng)建的。Executors提供方法以快捷的創(chuàng)建三種常用的 ThreadPoolExecutor:
- 固定大小的線程池
public static ExecutorService newFixedThreadPool(int nThreads){}
public static ExecutorService newFixedThreadPool(
int nThreads, ThreadFactory threadFactory){
}
- 單個(gè)線程的線程池
public static ExecutorService newSingleThreadExecutor(){}
public static ExecutorService newSingleThreadExecutor(
ThreadFactory threadFactory){
}
- 按需創(chuàng)建新線程的線程池
maximumPoolSize為Integer.MAX_VALUE,隊(duì)列類型為 SynchronousQueue.
public static ExecutorService newCachedThreadPool(){}
public static ExecutorService newCachedThreadPool(
ThreadFactory threadFactory){
}
2.3 ScheduledThreadPoolExecutor
簡單理解就是可調(diào)度版的ThreadPoolExecutor。一般是用來執(zhí)行周期性的可調(diào)度的任務(wù)的。
Executors提供方法以快捷的創(chuàng)建 ScheduledThreadPoolExecutor:
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize){}
public static ScheduledExecutorService newScheduledThreadPool(
int corePoolSize, ThreadFactory threadFactory) {
}
2.4 Runnable、Callable
這兩個(gè)接口表示可以被ScheduledThreadPoolExecutor或ThreadPoolExecutor執(zhí)行的任務(wù)的抽象。
在Excutors工廠中提供了將Runnable包裝為Callable的方法:
public static Callable<Object> callable(Runnable task){}
public static <T> Callable<T> callable(Runnable task, T result) {}
2.5 Future、FutureTask
Future表示異步執(zhí)行的任務(wù)的返回結(jié)果。FutureTask是Future接口的間接實(shí)現(xiàn)類。
其實(shí)FutureTask不僅實(shí)現(xiàn)了Future接口,同時(shí)還實(shí)現(xiàn)了Runnable接口。
FutureTask的狀態(tài):
- 未啟動(dòng):剛剛被創(chuàng)建,尚未調(diào)動(dòng)run()方法的狀態(tài)
- 已啟動(dòng):調(diào)用了run()方法
- 已完成:run方法執(zhí)行完畢
FutureTask類的常用方法:
- get() : Waits if necessary for the computation to complete, and then retrieves its result.
- 在未啟動(dòng)狀態(tài)調(diào)用get()==>阻塞
- 在已啟動(dòng)狀態(tài)調(diào)用get()==>阻塞
- 在已完成狀態(tài)調(diào)用get()==>返回執(zhí)行結(jié)果,或者拋出異常
- cancel():取消執(zhí)行
- 在未啟動(dòng)狀態(tài)調(diào)用cancel(),將導(dǎo)致該任務(wù)永遠(yuǎn)不會(huì)被執(zhí)行。
- 在已啟動(dòng)狀態(tài)調(diào)用cancel(),將會(huì)使用中斷該任務(wù)線程的方式來嘗試取消任務(wù)的執(zhí)行。
- 在調(diào)用時(shí)如果該任務(wù)已經(jīng)被終止、或已經(jīng)完成或者由于一些原因無法終止該方法都將返回false.
2.6 Executor、ExecutorService
java.util.concurrent.Executor是一個(gè)可以執(zhí)行提交給他的任務(wù)的對(duì)象的抽象接口。只包含一個(gè)方法:
void execute(Runnable command);
java.util.concurrent.ExecutorService是Executor的子接口,提供了更加豐富的接口方法。
3 簡單示例
ExecutorService service = Executors.newFixedThreadPool(12);
Future<List<String>> future = service.submit(new Callable<List<String>>() {
@Override
public List<String> call() throws Exception {
// 模擬查詢耗時(shí)3秒
Thread.sleep(3000);
return Arrays.asList("tom", "cat", "apache", "Spark", "Scala");
}
});
try {
List<String> list = future.get();
list.forEach(System.out::println);
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
} finally {
service.shutdown();
}
參考資料
- 《java并發(fā)編程的藝術(shù)》