#### 11.3.2 線程池的分類
在11.3.1節中對ThreadPoolExecutor的配置細節進行了詳細的介紹,本節將接著介紹**Android中最常見的四類具有不同功能特性的線程池,它們都直接或間接地通過配置ThreadPoolExecutor來實現自己的功能特性**,這四類線程池分別是**FixedThreadPool、CachedThreadPool、ScheduledThreadPool以及SingleThreadExecutor**。
在JDK 的java.util.concurrent.Executors 中提供了生成多種線程池的靜態方法,然后調用他們的execute 方法即可。
```
ExecutorService newCachedThreadPool = Executors.newCachedThreadPool();
ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(4);
ScheduledExecutorService newScheduledThreadPool =Executors.newScheduledThreadPool(4);
ExecutorService newSingleThreadExecutor = Executors.newSingleThreadExecutor();
```
* [ ] 1、**FixedThreadPooI**
通過[Executors](https://www.androidos.net.cn/android/6.0.1_r16/xref/libcore/luni/src/main/java/java/util/concurrent/Executors.java)的`newFixedThreadPool`方法來創建。它是一種**線程數量固定的線程池**,
* 當**線程處于空閑狀態時,它們并不會被回收,除非線程池被關閉了**。
* 當**所有的線程都處于活動狀態時,新任務都會處于等待狀態,直到有線程空閑出來**。
由于**FixedThreadPool只有核心線程并且這些核心線程不會被回收**,這意味著**它能夠更加快速地響應外界的請求**。newFixedThreadPool方法的實現如下,可以發現**FixedThreadPool中只有核心線程并且這些核心線程沒有超時機制,另外任務隊列也是沒有大小限制**的。
```
public static ExecutorService newFixedThreadPool(int nThreads) {
return new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
}
```
* [ ] 2、**CachedThreadPooI**
通過Executors的`newCachedThreadPool`方法來創建。
它是一種**線程數量不定的線程池,它只有非核心線程,并且其最大線程數為`Integer.MAX_VALUE`**。由于`Integer.MAX_VALUE`是一個很大的數,實際上就**相當于最大線程數可以任意大**。
**當線程池中的線程都處于活動狀態時,線程池會創建新的線程來處理新任務,否則就會利用空閑的線程來處理新任務。線程池中的空閑線程都有超時機制,這個超時時長為60秒,超過60秒閑置線程就會被回收**。
和FixedThreadPool不同的是,**CachedThreadPool的任務隊列其實相當于一個空集合,這將導致任何任務都會立即被執行,因為在這種場景下SynchronousQueue是無法插入任務的**。
*SynchronousQueue是一個非常特殊的隊列,在很多情況下可以把它簡單理解為一個無法存儲元素的隊列,由于它在實際中較少使用,這里就不深入探討它了*。從**CachedThreadPoo**l的特性來看,這類線程池**比較適合執行大量的耗時較少的任務。當整個線程池都處于閑置狀態時,線程池中的線程都會超時而被停止,這個時候CachedThreadPool之中實際上是沒有任何線程的,它幾乎是不占用任何系統資源的**。newCachedThreadPool方法的實現如下所示。
```
public static ExecutorService newCachedThreadPool() {
return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
60L, TimeUnit.SECONDS,
new SynchronousQueue<Runnable>());
}
```
* 3、**ScheduIedThreadPooI**
通過Executors的`newScheduledThreadPool`方法來創建。它的**核心線程數量是固定的,而非核心線程數是沒有限制的,并且當非核心線程閑置時會被立即回收**。ScheduledThreadPool這類線程池**主要用于執行定時任務和具有固定周期的重復任務**,newScheduledThreadPool方法的實現如下所示。
```
public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
return new ScheduledThreadPoolExecutor(corePoolSize);
}
public ScheduledThreadPoolExecutor(int corePoolSize) {
super(corePoolSize, Integer.MAX_VALUE, 0, NANOSECONDS,new DelayedWorkQueue());
}
```
* 4、**SingIeThreadExecutor**
通過Executors的`newSingleThreadExecutor`方法來創建。這類**線程池內部只有一個核心線程,它確保所有的任務都在同一個線程中按順序執行**。SingleThreadExecutor的**意義在于統一所有的外界任務到一個線程中,這使得在這些任務之間不需要處理線程同步的問題**。newSingleThreadExecutor方法的實現如下所示。
```
public static ExecutorService newSingleThreadExecutor() {
return new FinalizableDelegatedExecutorService
(new ThreadPoolExecutor(1, 1,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>()));
}
```
上面對Android中常見的4種線程池進行了詳細的介紹,除了上面系統提供的4類線程池以外,也可以根據實際需要靈活地配置線程池。下面的代碼演示了系統預置的4種線程池的典型使用方法。
```
Runnable command = new Runnable() {
@Override
public void run() {
SystemClock.sleep(2000);
}
};
ExecutorService fixedThreadPool = Executors.newFixedThreadPool(4);
fixedThreadPool.execute(command);
ExecutorService cachedThreadPool = Executors.newCachedThreadPool();
cachedThreadPool.execute(command);
ScheduledExecutorService scheduledThreadPool=Executors.newScheduledThreadPool(4);
// 2000ms后執行command
scheduledThreadPool.schedule(command, 2000, TimeUnit.MILLISECONDS);
// 延遲10ms后,每隔1000ms執行一次command
scheduledThreadPool.scheduleAtFixedRate(command, 10, 1000, TimeUnit.MILLISECONDS);
ExecutorService singleThreadExecutor =Executors.newSingleThreadExecutor();
singleThreadExecutor.execute(command);
```
- 前言
- 第1章 Activity的生命周期和啟動模式
- 1.1 Activity的生命周期全面分析
- 1.1.1 典型情況下的生命周期分析
- 1.1.2 異常情況下的生命周期分析
- 1.2 Activity的啟動模式
- 1.2.1 Activity的LaunchMode
- 1.2.2 Activity的Flags
- 1.3 IntentFilter的匹配規則
- 第2章 IPC機制
- 2.1 Android IPC簡介
- 2.2 Android中的多進程模式
- 2.2.1 開啟多進程模式
- 2.2.2 多進程模式的運行機制
- 2.3 IPC基礎概念介紹
- 2.3.1 Serializable接口
- 2.3.2 Parcelable接口
- 2.3.3 Binder
- 2.4 Android中的IPC方式
- 2.4.1 使用Bundle
- 2.4.2 使用文件共享
- 2.4.3 使用Messenger
- 2.4.4 使用AIDL
- 2.4.5 使用ContentProvider
- 2.4.6 使用Socket
- 2.5 Binder連接池
- 2.6 選用合適的IPC方式
- 第3章 View的事件體系
- 3.1 View基礎知識
- 3.1.1 什么是View
- 3.1.2 View的位置參數
- 3.1.3 MotionEvent和TouchSlop
- 3.1.4 VelocityTracker、GestureDetector和Scroller
- 3.2 View的滑動
- 3.2.1 使用scrollTo/scrollBy
- 3.2.2 使用動畫
- 3.2.3 改變布局參數
- 3.2.4 各種滑動方式的對比
- 3.3 彈性滑動
- 3.3.1 使用Scroller7
- 3.3.2 通過動畫
- 3.3.3 使用延時策略
- 3.4 View的事件分發機制
- 3.4.1 點擊事件的傳遞規則
- 3.4.2 事件分發的源碼解析
- 3.5 View的滑動沖突
- 3.5.1 常見的滑動沖突場景
- 3.5.2 滑動沖突的處理規則
- 3.5.3 滑動沖突的解決方式
- 第4章 View的工作原理
- 4.1 初識ViewRoot和DecorView
- 4.2 理解MeasureSpec
- 4.2.1 MeasureSpec
- 4.2.2 MeasureSpec和LayoutParams的對應關系
- 4.3 View的工作流程
- 4.3.1 measure過程
- 4.3.2 layout過程
- 4.3.3 draw過程
- 4.4 自定義View
- 4.4.1 自定義View的分類
- 4.4.2 自定義View須知
- 4.4.3 自定義View示例
- 4.4.4 自定義View的思想
- 第5章 理解RemoteViews
- 5.1 RemoteViews的應用
- 5.1.1 RemoteViews在通知欄上的應用
- 5.1.2 RemoteViews在桌面小部件上的應用
- 5.1.3 PendingIntent概述
- 5.2 RemoteViews的內部機制
- 5.3 RemoteViews的意義
- 第6章 Android的Drawable
- 6.1 Drawable簡介
- 6.2 Drawable的分類
- 6.2.1 BitmapDrawable2
- 6.2.2 ShapeDrawable
- 6.2.3 LayerDrawable
- 6.2.4 StateListDrawable
- 6.2.5 LevelListDrawable
- 6.2.6 TransitionDrawable
- 6.2.7 InsetDrawable
- 6.2.8 ScaleDrawable
- 6.2.9 ClipDrawable
- 6.3 自定義Drawable
- 第7章 Android動畫深入分析
- 7.1 View動畫
- 7.1.1 View動畫的種類
- 7.1.2 自定義View動畫
- 7.1.3 幀動畫
- 7.2 View動畫的特殊使用場景
- 7.2.1 LayoutAnimation
- 7.2.2 Activity的切換效果
- 7.3 屬性動畫
- 7.3.1 使用屬性動畫
- 7.3.2 理解插值器和估值器 /
- 7.3.3 屬性動畫的監聽器
- 7.3.4 對任意屬性做動畫
- 7.3.5 屬性動畫的工作原理
- 7.4 使用動畫的注意事項
- 第8章 理解Window和WindowManager
- 8.1 Window和WindowManager
- 8.2 Window的內部機制
- 8.2.1 Window的添加過程
- 8.2.2 Window的刪除過程
- 8.2.3 Window的更新過程
- 8.3 Window的創建過程
- 8.3.1 Activity的Window創建過程
- 8.3.2 Dialog的Window創建過程
- 8.3.3 Toast的Window創建過程
- 第9章 四大組件的工作過程
- 9.1 四大組件的運行狀態
- 9.2 Activity的工作過程
- 9.3 Service的工作過程
- 9.3.1 Service的啟動過程
- 9.3.2 Service的綁定過程
- 9.4 BroadcastReceiver的工作過程
- 9.4.1 廣播的注冊過程
- 9.4.2 廣播的發送和接收過程
- 9.5 ContentProvider的工作過程
- 第10章 Android的消息機制
- 10.1 Android的消息機制概述
- 10.2 Android的消息機制分析
- 10.2.1 ThreadLocal的工作原理
- 10.2.2 消息隊列的工作原理
- 10.2.3 Looper的工作原理
- 10.2.4 Handler的工作原理
- 10.3 主線程的消息循環
- 第11章 Android的線程和線程池
- 11.1 主線程和子線程
- 11.2 Android中的線程形態
- 11.2.1 AsyncTask
- 11.2.2 AsyncTask的工作原理
- 11.2.3 HandlerThread
- 11.2.4 IntentService
- 11.3 Android中的線程池
- 11.3.1 ThreadPoolExecutor
- 11.3.2 線程池的分類
- 第12章 Bitmap的加載和Cache
- 12.1 Bitmap的高效加載
- 12.2 Android中的緩存策略
- 12.2.1 LruCache
- 12.2.2 DiskLruCache
- 12.2.3 ImageLoader的實現446
- 12.3 ImageLoader的使用
- 12.3.1 照片墻效果
- 12.3.2 優化列表的卡頓現象
- 第13章 綜合技術
- 13.1 使用CrashHandler來獲取應用的crash信息
- 13.2 使用multidex來解決方法數越界
- 13.3 Android的動態加載技術
- 13.4 反編譯初步
- 13.4.1 使用dex2jar和jd-gui反編譯apk
- 13.4.2 使用apktool對apk進行二次打包
- 第14章 JNI和NDK編程
- 14.1 JNI的開發流程
- 14.2 NDK的開發流程
- 14.3 JNI的數據類型和類型簽名
- 14.4 JNI調用Java方法的流程
- 第15章 Android性能優化
- 15.1 Android的性能優化方法
- 15.1.1 布局優化
- 15.1.2 繪制優化
- 15.1.3 內存泄露優化
- 15.1.4 響應速度優化和ANR日志分析
- 15.1.5 ListView和Bitmap優化
- 15.1.6 線程優化
- 15.1.7 一些性能優化建議
- 15.2 內存泄露分析之MAT工具
- 15.3 提高程序的可維護性