前言
多線程并發(fā)是我們在開發(fā)中經(jīng)常遇到的問題,提及線程池,首先我們得了解線程的相關(guān)知識良蒸。關(guān)于線程的詳情介紹本文就不提及了技扼,有不太清楚的朋友可以自行查找相關(guān)資料,下面簡要概述一下進(jìn)程和線程的概念嫩痰,為后續(xù)內(nèi)容(線程池)做鋪墊剿吻。
進(jìn)程:
每個app運(yùn)行時前首先創(chuàng)建一個進(jìn)程,該進(jìn)程是由Zygote fork出來的串纺,用于承載App上運(yùn)行的各種Activity/Service等組件丽旅。
進(jìn)程對于上層應(yīng)用來說是完全透明的,這也是google有意為之纺棺,讓App程序都是運(yùn)行在Android Runtime榄笙。大多數(shù)情況一個App就運(yùn)行在一個進(jìn)程中,除非在AndroidManifest.xml中配置Android:process屬性祷蝌,或通過native代碼fork進(jìn)程茅撞。
線程:
線程對應(yīng)用來說非常常見,比如每次new Thread().start都會創(chuàng)建一個新的線程杆逗。該線程與App所在進(jìn)程之間資源共享乡翅,從Linux角度來說進(jìn)程與線程除了是否共享資源外,并沒有本質(zhì)的區(qū)別罪郊,都是一個task_struct結(jié)構(gòu)體蠕蚜,在CPU看來進(jìn)程或線程無非就是一段可執(zhí)行的代碼,CPU采用CFS調(diào)度算法悔橄,保證每個task都盡可能公平的享有CPU時間片靶累。
本文就以下幾個問題展開講解:
線程池的基本概念。
采用線程池的優(yōu)勢癣疟。
Android 中常用的幾種線程池挣柬。
如何終止某個線程任務(wù)。
一睛挚、關(guān)于線程池
Android中的線程池的概念來源于Java中的Executor邪蛔,它們的使用基本是一致的。Executor是一個接口扎狱,真正的線程池的實(shí)現(xiàn)為ThreadPoolExecutor侧到。ThreadPoolExecutor提供了一系列參數(shù)來配置線程池,Android中常用的幾種線程池都是通過對ThreadPoolExecutor進(jìn)行不同配置來實(shí)現(xiàn)的淤击。
ThreadPoolExecutor 構(gòu)造方法
ThreadPoolExecutor 有多個重載方法匠抗,但最終都調(diào)用了這個構(gòu)造方法:
public ThreadPoolExecutor(int corePoolSize,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int maximumPoolSize,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? long keepAliveTime,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TimeUnit unit,
? ? ? ? ? ? ? ? ? ? BlockingQueue<Runnable> workQueue,
? ? ? ? ? ? ? ? ? ? ? ? ? ThreadFactory threadFactory)
我們可以看到,這個構(gòu)造方法里一共有7個參數(shù)污抬,其參數(shù)的含義如下:
corePoolSize: 線程池中核心線程的數(shù)量汞贸。
maximumPoolSize: 線程池中最大線程數(shù)量。
keepAliveTime: 非核心線程的超時時長,當(dāng)系統(tǒng)中非核心線程閑置時間超過keepAliveTime之后矢腻,則會被回收门驾。如果ThreadPoolExecutor的allowCoreThreadTimeOut屬性設(shè)置為true,則該參數(shù)也表示核心線程的超時時長踏堡。
unit: keepAliveTime這個參數(shù)的單位猎唁,有納秒、微秒顷蟆、毫秒诫隅、秒、分帐偎、時逐纬、天等。
workQueue: 線程池中的任務(wù)隊列削樊,該隊列主要用來存儲已經(jīng)被提交但是尚未執(zhí)行的任務(wù)豁生。存儲在這里的任務(wù)是由ThreadPoolExecutor的execute方法提交來的。
threadFactory: 為線程池提供創(chuàng)建新線程的功能漫贞,這個我們一般使用默認(rèn)即可甸箱。
handler: 拒絕策略,當(dāng)線程無法執(zhí)行新任務(wù)時(一般是由于線程池中的線程數(shù)量已經(jīng)達(dá)到最大數(shù)或者線程池關(guān)閉導(dǎo)致的)迅脐,默認(rèn)情況下芍殖,當(dāng)線程池?zé)o法處理新線程時,會拋出一個RejectedExecutionException谴蔑。
兩個執(zhí)行的方法
ThreadPoolExecutor有兩個方法可以供我們執(zhí)行豌骏,分別是submit()和execute(),我們先來看看這兩個方法到底有什么差異
execute()方法源碼:
public void execute(Runnable command) {
? ? ? ? if (command == null)
? ? ? ? ? ? throw new NullPointerException();
? ? ? ? //獲得當(dāng)前線程的生命周期對應(yīng)的二進(jìn)制狀態(tài)碼
? ? ? ? int c = ctl.get();
? ? ? ? //判斷當(dāng)前線程數(shù)量是否小于核心線程數(shù)量,如果小于就直接創(chuàng)建核心線程執(zhí)行任務(wù),創(chuàng)建成功直接跳出,失敗則接著往下走.
? ? ? ? if (workerCountOf(c) < corePoolSize) {
? ? ? ? ? ? if (addWorker(command, true))
? ? ? ? ? ? ? ? return;
? ? ? ? ? ? c = ctl.get();
? ? ? ? }
? ? ? ? //判斷線程池是否為RUNNING狀態(tài),并且將任務(wù)添加至隊列中.
? ? ? ? if (isRunning(c) && workQueue.offer(command)) {
? ? ? ? ? ? int recheck = ctl.get();
? ? ? ? ? ? //審核下線程池的狀態(tài),如果不是RUNNING狀態(tài),直接移除隊列中
? ? ? ? ? ? if (! isRunning(recheck) && remove(command))
? ? ? ? ? ? ? ? reject(command);
? ? ? ? ? ? ? ? //如果當(dāng)前線程數(shù)量為0,則單獨(dú)創(chuàng)建線程,而不指定任務(wù).
? ? ? ? ? ? else if (workerCountOf(recheck) == 0)
? ? ? ? ? ? ? ? addWorker(null, false);
? ? ? ? }
? ? ? ? //如果不滿足上述條件,嘗試創(chuàng)建一個非核心線程來執(zhí)行任務(wù),如果創(chuàng)建失敗,調(diào)用reject()方法.
? ? ? ? else if (!addWorker(command, false))
? ? ? ? ? ? reject(command);
? ? }
submit()方法源碼:
public <T> Future<T> submit(Callable<T> task) {
? ? ? ? if (task == null) throw new NullPointerException();
? ? ? ? RunnableFuture<T> ftask = newTaskFor(task);
? ? ? ? //還是通過調(diào)用execute
? ? ? ? execute(ftask);
? ? ? ? //最后會將包裝好的Runable返回
? ? ? ? return ftask;
? ? }
//將Callable<T> 包裝進(jìn)FutureTask中
? ? protected <T> RunnableFuture<T> newTaskFor(Callable<T> callable) {
? ? ? ? return new FutureTask<T>(callable);
? ? }
//可以看出FutureTask也是實(shí)現(xiàn)Runnable接口,因為RunableFuture本身就繼承了Runnabel接口
public class FutureTask<V> implements RunnableFuture<V> {
? ? .......
}
public interface RunnableFuture<V> extends Runnable, Future<V> {
? ? /**
? ? * Sets this Future to the result of its computation
? ? * unless it has been cancelled.
? ? */
? ? void run();
}
從上面兩個方法的源碼我們可以分析出幾個結(jié)論隐锭,
submit()其實(shí)還是需要調(diào)用execute()去執(zhí)行任務(wù)的,不同是submit()將包裝好的任務(wù)進(jìn)行了返回窃躲,他會返回一個Future對象。
從execute()方法中,不難看出addWorker()方法, 是創(chuàng)建線程(核心線程,非核心線程)的主要方法,而reject()方法為線程創(chuàng)建失敗的回調(diào)钦睡。
所以蒂窒,通常情況下,在不需要線程執(zhí)行返回結(jié)果值時荞怒,我們使用execute 方法洒琢。 而當(dāng)我們需要返回值時,則使用submit方法挣输,他會返回一個Future對象纬凤。Future不僅僅可以獲得一個結(jié)果福贞,他還可以被取消撩嚼,我們可以通過調(diào)用future的cancel()方法,取消一個Future的執(zhí)行。 比如我們加入了一個線程完丽,但是在這過程中我們又想中斷它恋技,則可通過sumbit 來實(shí)現(xiàn)。
二逻族、采用線程池的優(yōu)勢蜻底?
1. 避免線程頻繁創(chuàng)建消毀。
雖然采用Thread 創(chuàng)建線程可以實(shí)現(xiàn)耗時操作聘鳞,但線程的大量創(chuàng)建和銷毀薄辅,會造成過大的性能開銷。
2.避免系統(tǒng)資源緊張抠璃。
當(dāng)大量的線程一起運(yùn)作的時候站楚,可能會造成資源緊張,上面也介紹過線程底層的機(jī)制就是切分CPU的時間搏嗡,而大量的線程同時存在時可能造成互相搶占資源的現(xiàn)象發(fā)生窿春,從而導(dǎo)致阻塞的現(xiàn)象。
3.更好地管理線程采盒。
以下載功能為例旧乞,一般情況下,會有限制最大并發(fā)下載數(shù)目磅氨,而利用線程池我們可以靈活根據(jù)實(shí)際需求來設(shè)置同時下載的最大量尺栖、串行執(zhí)行下載任務(wù)順序、實(shí)現(xiàn)隊列等待等功能悍赢。
三决瞳、Android 中常用的幾種線程池。
3.1 FixedThreadPool
它的源碼如下:
public static ExecutorService newFixedThreadPool(int nThreads) {
? ? ? ? return new ThreadPoolExecutor(nThreads, nThreads,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0L, TimeUnit.MILLISECONDS,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new LinkedBlockingQueue<Runnable>());
? ? }
從源碼我們可以看出兩個特征:
1.它只有一個傳入?yún)?shù)左权,即固定核心線程數(shù)
它只提供了一個nThreads皮胡,供外部傳入進(jìn)來,并且它的核心線程數(shù)和最大線程數(shù)是一樣的赏迟。這說明在FixedThreadPool中沒有非核心線程屡贺,所有的線程都是核心線程。
2. 線程的超時時間為0锌杀。
這說明核心線程即使在沒有任務(wù)可執(zhí)行的時候甩栈,也不會被銷毀,這樣可讓FixedThreadPool更快速的響應(yīng)請求糕再。最后的線程隊列是一個LinkedBlockingQueue量没,但是LinkedBlockingQueue卻沒有參數(shù),這說明線程隊列的大小為Integer.MAX_VALUE(2^31 - 1)
從以上源碼參數(shù)我們對FixedThreadPool的工作特點(diǎn)應(yīng)該也有大體的理解了突想,接下來我們繼續(xù)分析其他幾個線程池殴蹄。
3.2 SingleThreadExecutor
它的源碼如下:
public static ExecutorService newSingleThreadExecutor() {?
? ? return new FinalizableDelegatedExecutorService?
? ? ? ? (new ThreadPoolExecutor(1, 1,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 0L, TimeUnit.MILLISECONDS,?
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new LinkedBlockingQueue<Runnable>()));?
從源碼我們可以很容易發(fā)現(xiàn) SingleThreadExecutor和FixedThreadPool很像究抓,不同的是SingleThreadExecutor的核心線程數(shù)只有1, 也就是只能同時有一個線程被執(zhí)行。使用它可以避免我們處理線程同步問題袭灯。
打個比喻刺下,它就類似于排隊買票,一次只能同時處理一個人的事務(wù)稽荧。其實(shí)如果我們把FixedThreadPool的參數(shù)傳為1橘茉,就和SingleThreadExecutor的作用一致了。
3.3 CachedThreadPool
它的源碼如下:
? public static ExecutorService newCachedThreadPool() {
? ? ? ? return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 60L, TimeUnit.SECONDS,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? new SynchronousQueue<Runnable>());
? ? }
從源碼可以看到姨丈,CachedThreadPool中是沒有核心線程的畅卓,但是它的最大線程數(shù)卻為Integer.MAX_VALUE,另外蟋恬,CachedThreadPool是有線程超時機(jī)制的髓介,它的超時時間為60秒。
由于最大線程數(shù)為無限大筋现,所以每當(dāng)添加一個新任務(wù)進(jìn)來的時候唐础,如果線程池中有空閑的線程,則由該空閑的線程執(zhí)行新任務(wù)矾飞;如果沒有空閑線程一膨,則創(chuàng)建新線程來執(zhí)行任務(wù)。
根據(jù)CachedThreadPool的特點(diǎn)洒沦,在有大量耗時短的任務(wù)請求時豹绪,可使用CachedThreadPool,因為當(dāng)CachedThreadPool中沒有新任務(wù)的時候申眼,它里邊所有的線程都會因為60秒超時而被終止瞒津。
3.4 ScheduledThreadPool
它的源碼如下:
public ScheduledThreadPoolExecutor(int corePoolSize) {?
? ? super(corePoolSize, Integer.MAX_VALUE,?
? ? ? ? ? DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,?
? ? ? ? ? new DelayedWorkQueue());?
}
從源碼可以看出,它的核心線程數(shù)量是固定的括尸,但是非核心線程無窮大巷蚪。當(dāng)非核心線程閑置時,則會被立即回收濒翻。
ScheduledThreadPool也是四個當(dāng)中唯一一個具有定時定期執(zhí)行任務(wù)功能的線程池屁柏。它適合執(zhí)行一些周期性任務(wù)或者延時任務(wù)。
延時啟動任務(wù)示例:
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
? ? ? ? ? ? Runnable runnable = new Runnable(){
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? ? ? //TODO method();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ?
? ? ? ? //延遲一秒執(zhí)行
? ? ? ? scheduledExecutorService.schedule(runnable, 1, TimeUnit.SECONDS);
延時周期啟動任務(wù)示例:
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(2);
? ? ? ? ? ? Runnable runnable = new Runnable(){
? ? ? ? ? ? ? ? @Override
? ? ? ? ? ? ? ? public void run() {
? ? ? ? ? ? ? ? ? ? //TODO method();
? ? ? ? ? ? ? ? }
? ? ? ? ? ? };
? ? ? ? //延遲三秒后有送,執(zhí)行周期一秒的定時任務(wù)
? ? ? ? scheduledExecutorService.scheduleAtFixedRate(runnable, 3, 1, TimeUnit.SECONDS);
四淌喻、如何終止線程池中的某個線程任務(wù)?
一般線程執(zhí)行完run方法之后雀摘,線程就正常結(jié)束了裸删,因此有如下幾種方式來實(shí)現(xiàn):
4.1 利用 Future 和 Callable。
步驟:
實(shí)現(xiàn) Callable 接口
調(diào)用 pool.submit() 方法阵赠,返回 Future 對象
用 Future 對象來獲取線程的狀態(tài)涯塔。
private void cancelAThread() {
? ? ? ? ExecutorService pool = Executors.newFixedThreadPool(2);
? ? ? ? ?
? ? ? ? ? Callable<String> callable = new Callable<String>() {
? ? ? ? ? ? ?
? ? ? ? ? ? @Override
? ? ? ? ? ? public String call() throws Exception {
? ? ? ? ? ? ? ? System.out.println("test");
? ? ? ? ? ? ? ? return "true";
? ? ? ? ? ? }
? ? ? ? };
? ? ? ? ?
? ? ? ? Future<String> f = pool.submit(callable);
? ? ? ? ?
? ? ? ? System.out.println(f.isCancelled());
? ? ? ? System.out.println(f.isDone());
? ? ? ? f.cancel(true);
?
? ? }
關(guān)于 Future 和 Callable 的介紹乘粒,推薦看這篇文章,內(nèi)容很詳細(xì): 《Android并發(fā)編程之白話文詳解Future,FutureTask和Callable》
4.2 利用 volatile 關(guān)鍵字伤塌,設(shè)置退出flag, 用于終止線程轧铁。
public class ThreadSafe extends Thread {
? ? public volatile boolean isCancel = false;
? ? ? ? public void run() {
? ? ? ? while (!isCancel){
? ? ? ? ? //TODO method();
? ? ? ? }
? ? }
}
4.3 interrupt()方法終止線程,并捕獲異常每聪。
public class ThreadSafe extends Thread {
?
? @Override
? ? public void run() {
? ? ? ? while (!isInterrupted()){ //非阻塞過程中通過判斷中斷標(biāo)志來退出
? ? ? ? ? ? try{
? ? ? ? ? ? ? //TODO method();
? ? ? ? ? ? ? //阻塞過程捕獲中斷異常來退出
? ? ? ? ? ? }catch(InterruptedException e){
? ? ? ? ? ? ? ? e.printStackTrace();
? ? ? ? ? ? ? ? break;//捕獲到異常之后,執(zhí)行break跳出循環(huán)齿风。
? ? ? ? ? ? }
? ? ? ? }
? ? }
}