Android的線程和線程池

線程分為主線程和子線程,主線程主要處理和界面相關的事情,而子線程則往往用于執行耗時操作。

AsyncTask封裝了線程池和Handler,它主要是為了方便開發者在子線程中更新UI。HandlerThread是一種具有消息循環的線程,在它的內部可以使用Handler。IntentService是一個服務,系統對其進行了封裝使其可以更方便的執行后臺任務,IntentService內部采用HandlerThread來執行任務,當任務執行完畢后IntentService會自動退出。從任務執行的角度來看,IntentService的作用很像一個后臺進程,但是IntentService是一種服務,它不容易被系統殺死從而可以盡量保證任務的執行

AsyncTask

AsyncTask是一種輕量級的異步任務類,它可以在線程池中執行后臺任務,然后把執行的進度和最終結果傳遞給主線程并在主線程中更新UI。AsyncTask封裝了Thread和Handler,但是并不是和進行特別耗時的后臺任務。對于特別耗時的后臺任務,推薦使用線程池。

AsyncTask是一個抽象的泛型類,它提供了Params、Progress和Resul這三個泛型參數,其中Params表示參數的類型,Progress表示后臺任務的執行進度的類型,而Result則表示后臺任務的返回結果的類型,如果AsyncTask缺失不需要傳遞具體的參數,那么這三個泛型參數都可以使用Void來代替。AsyncTask的聲明如下。

public abstract class AsyncTask<Params, Progress, Result> 

AsyncTask提供了4個核心方法:

  1. onPreExecute(),在主線程中執行,在異步任務執行之前,此方法會被調用,用于做準備工作。
  2. doInBackground(Params... params),在線程池中執行,此方法用于執行異步任務,params參數表示異步任務的輸入參數。在此方法中可以通過publishProgress方法來更新任務的進度,publishProgress方法會調用onProgressUpdate方法。另外此方法需要返回計算結果給onPostExecute方法。
  3. onProgressUpdate(Progress... values),在主線程中執行,當后臺任務的執行進度發生改變時此方法會被調用。
  4. onPostExecute(Result result),在主線程中執行,在異步任務執行之后,此方法會被調用,其中result參數是后臺任務的返回值,即doInBackground的返回值。

上面這幾個方法,onPreExecute先執行,接著是doInBackground,最后是onPostExecute。除此之外還提供了onCancelled()方法,同樣也是在主線程中調用,當異步任務被取消時,onCancelled()方法會被調用,此時onPostExecute不會被調用。

public void onClick(View v) {
    new DownloadImageTask().execute("http://example.com/image.png");
}

private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    /** The system calls this to perform work in a worker thread and
      * delivers it the parameters given to AsyncTask.execute() */
    protected Bitmap doInBackground(String... urls) {
        return loadImageFromNetwork(urls[0]);
    }

    /** The system calls this to perform work in the UI thread and delivers
      * the result from doInBackground() */
    protected void onPostExecute(Bitmap result) {
        mImageView.setImageBitmap(result);
    }
}

AsyncTask在具體的使用過程中有如下需要注意的地方:

  1. AsyncTask的類必須在主線程中加載。
  2. AsyncTask的對象必須在主線程中創建。
  3. execute方法不惜在UI線程調用。
  4. 不要在程序中直接調用onPreExecute()、onPostExecute()、doInBackground 和 onProgressUpdate方法。
  5. 一個AsyncTask對象只能執行一次,即只能調用一次execute方法,否則會報運行時異常。
  6. 在Android1.6之前,AsyncTask是串行執行任務的,Android1.6的時候AsyncTask開始采用線程池處理并行任務,在Android3.0開始,為了避免AsyncTask所帶來的并發錯誤,AsyncTask又采用一個線程來串行執行任務,但可以通過AsyncTask的executeOnExecutor方法來并行的執行任務。

官網上關于AsyncTask的說明

HandlerThread

HandlerThread繼承了Thread,它是一種可以使用Handler的Thread,它的實現是在run方法中通過Looper.prepare()來創建消息隊列,并通過Looper.loop()來開啟消息循環,這樣在實際的使用中就允許在HandlerThread中創建Handler了。HandlerThread的run方法如下:

    public void run() {
        mTid = Process.myTid();
        Looper.prepare();
        synchronized (this) {
            mLooper = Looper.myLooper();
            notifyAll();
        }
        Process.setThreadPriority(mPriority);
        onLooperPrepared();
        Looper.loop();
        mTid = -1;
    }

從HandlerThread的實現來看,它和普通的Thread有顯著的不同之處。普通Thread主要用于在run方法中執行一個耗時任務,而HandlerThread在內部創建了消息隊列,外界需要通過Handler的消息方式來通知HandlerThread執行一個具體的任務。由于HandlerThread的run方法是一個無限循環(loop是一個無限循環,run里面調用了Looper.loop(),所以run也是無限循環),因此當明確不需要再使用HandlerThread時,可以通過quit或者quitSafely方法來終止線程的執行。

官網上關于HandlerThread的說明

IntentService

IntentService是一種特殊的Service,它繼承了Service并且它是一個抽象類,因此必須創建它的子類才能使用IntentService。IntentService可用于執行后臺耗時的任務,當任務執行后它會自動停止,同時由于IntentService是服務,優先級比單純的線程要高很多,所以更適合執行一些高優先級的后臺任務。在實現上,IntentService封裝了HandlerThread和Handler,可以從onCreate方法中看出,如下:

    @Override
    public void onCreate() {
        // TODO: It would be nice to have an option to hold a partial wakelock
        // during processing, and to have a static startService(Context, Intent)
        // method that would launch the service & hand off a wakelock.

        super.onCreate();
        HandlerThread thread = new HandlerThread("IntentService[" + mName + "]");
        thread.start();

        mServiceLooper = thread.getLooper();
        mServiceHandler = new ServiceHandler(mServiceLooper);
    }

當IntentService被第一次啟動時,它的onCreate方法會被調用,onCreate方法會創建一個HandlerThread,然后使用它的Looper來構造一個Handler對象mServiceHandler,這樣通過mServiceHandler發送的消息最終都會在HandlerThread中執行。IntentService是順序執行后臺任務的。在Android8.0及以上版本,建議使用JobIntentService
官網上關于IntentService的說明

Android中的線程池

線程池的優點:

  1. 重用線程池中的線程,避免弦音線程的創建和銷毀所帶來的性能開銷。
  2. 能有效控制線程池的最大并發數,避免大量的線程之間因互相搶占系統資源而導致的阻塞現象。
  3. 能過對線程進行簡單的管理,并提供定時執行以及指定間隔循環執行等功能。

Android中的線程池的概念來源于Java中的Executor,Executor是一個接口,真正的線程池的實現為ThreadPoolExecutor。 ThreadPoolExecutor提供了一系列參數來配置線程池,通過不同的參數可以創建不同的線程池,從線程池的功能特性來說,Android的線程池主要分為4類,這4類線程池可以通過Executors所提供的工廠方法來得到。

ThreadPoolExecutor是線程池的真正實現,它的構造方法提供了一系列參數來配置線程池。它提供了4個構造方法,以下面這個來說明各個參數的含義。

public ThreadPoolExecutor(int corePoolSize,
                              int maximumPoolSize,
                              long keepAliveTime,
                              TimeUnit unit,
                              BlockingQueue<Runnable> workQueue,
                              ThreadFactory threadFactory)     

corePoolSize 線程池的核心線程數,默認情況下,核心線程會在線程池中一直存活,即使它們處于閑置狀態。

maximumPoolSize線程池所能容納的最大線程數,當活動線程數達到這個數值后,后續的新任務將會被阻塞。

keepAliveTime非核心線程閑置時的超時時長,超過這個時長,非核心線程就會被回收。當ThreadPoolExecutor的allowCoreThreadTimeOut屬性設置為true時,keepAliveTime同樣會作用于核心線程。

unit用于指定keepAliveTime參數的時間單位,是一個枚舉,常用的有TimeUnit.MILLISECONDS、TimeUnit.SECONDS、TimeUnit.MINUTE等。

workQueue線程池中的任務隊列,通過線程池的execute方法提交的Runable對象會存儲在這個參數中。

threadFactory線程工廠,為線程池提供創建新線程的功能。ThreadFactory是一個接口,它只有一個方法:Thread newThread(Runable r)。

ThreadPoolExecutor執行任務時大致遵循如下規則:

  1. 如果線程池中的線程數量未達到核心線程的數量,那么會直接啟動一個核心線程來執行任務。
  2. 如果線程池中的線程數量已經達到或者超過核心線程的數量,那么任務會被插入到任務隊列中排隊等待執行。
  3. 如果在步驟2中無法將任務插入到任務隊列中,這往往死由于任務隊列已滿,這個時候如果線程數量未達到線程池規定的最大值,那么會立刻啟動給一個非核心線程來執行任務。
  4. 如果步驟3中線程數量已經達到線程池規定的最大值,那么就拒絕執行任務,ThreadPoolExecutor會調用RejectedExecutionHandler的rejectedExecution方法來通知調用者。

官網上關于ThreadPoolExecutor的說明

常見的線程池

Android中最常見的四類具有不同功能特性的線程池,都直接或者間接的通過配置ThreadPoolExecutor來實現自己的功能特性。

FixedThreadPool 通過Executors的newFixedThredPool方法來創建。它是一種線程數量固定的線程池,當線程處于空閑狀態時,它們并不會被回收,除非線程池被關閉了。當所有的線程都處于活動狀態時,新任務都會處于等待狀態,直到有線程空閑出來。由于FixedThreadPool只有核心線程并且這些核心線程不會被回收,這意味著它能夠更加快速的響應外界的請求

    /**
     * Creates a thread pool that reuses a fixed number of threads
     * operating off a shared unbounded queue.  At any point, at most
     * {@code nThreads} threads will be active processing tasks.
     * If additional tasks are submitted when all threads are active,
     * they will wait in the queue until a thread is available.
     * If any thread terminates due to a failure during execution
     * prior to shutdown, a new one will take its place if needed to
     * execute subsequent tasks.  The threads in the pool will exist
     * until it is explicitly {@link ExecutorService#shutdown shutdown}.
     *
     * @param nThreads the number of threads in the pool
     * @return the newly created thread pool
     * @throws IllegalArgumentException if {@code nThreads <= 0}
     */
    public static ExecutorService newFixedThreadPool(int nThreads) {
        return new ThreadPoolExecutor(nThreads, nThreads,
                                      0L, TimeUnit.MILLISECONDS,
                                      new LinkedBlockingQueue<Runnable>());
    }

FixedThreadPool只有核心線程并且這些核心線程沒有超時機制,另外任務隊列也沒有大小限制。

CachedThreadPool通過Executors的newCachedThreadPool方法來創建。它是一種線程數量不定的線程池,它只有非核心線程,并且其最大線程數為Integer.MAX_VALUE。由于
Integer.MAX_VALUE是一個很大的數,實際上就相當于最大線程數可以任意大。當線程池中的線程都處于活動狀態時,線程池會創建新的線程來處理新任務,否則就會利用空閑的線程來處理新任務。線程池中的空閑線程都有超時機制,這個超時時長為60秒,超過60秒限制線程就會被回收,CachedThreadPool的任務隊列相當于一個空集合,這將導致任何任務都會被立即執行,因為在這種場景下SyncchornousQueue是無法插入任務的。比較適合執行大量的耗時較少的任務。當整個線程池都處于閑置狀態時,線程池中的線程都會超時而被停職,這個時候CachedThreadPool之中實際上是沒有任何線程的,它幾乎是不占任何系統資源的。

    /**
     * Creates a thread pool that creates new threads as needed, but
     * will reuse previously constructed threads when they are
     * available.  These pools will typically improve the performance
     * of programs that execute many short-lived asynchronous tasks.
     * Calls to {@code execute} will reuse previously constructed
     * threads if available. If no existing thread is available, a new
     * thread will be created and added to the pool. Threads that have
     * not been used for sixty seconds are terminated and removed from
     * the cache. Thus, a pool that remains idle for long enough will
     * not consume any resources. Note that pools with similar
     * properties but different details (for example, timeout parameters)
     * may be created using {@link ThreadPoolExecutor} constructors.
     *
     * @return the newly created thread pool
     */
    public static ExecutorService newCachedThreadPool() {
        return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                                      60L, TimeUnit.SECONDS,
                                      new SynchronousQueue<Runnable>());
    }

ScheduledThreadPool通過Executors的newScheduledThreadPool方法來創建。它的核心線程數量是固定的,而非核心線程數是沒有限制的,并且當非核心線程閑置時會被立即回收。ScheduledThreadPool這類線程池主要用于執行定時任務和具有固定周期的重復任務

    /**
     * Creates a thread pool that can schedule commands to run after a
     * given delay, or to execute periodically.
     * @param corePoolSize the number of threads to keep in the pool,
     * even if they are idle
     * @return a newly created scheduled thread pool
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
     */
    public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
        return new ScheduledThreadPoolExecutor(corePoolSize);
    }
    
    private static final long DEFAULT_KEEPALIVE_MILLIS = 10L;

    /**
     * Creates a new {@code ScheduledThreadPoolExecutor} with the
     * given core pool size.
     *
     * @param corePoolSize the number of threads to keep in the pool, even
     *        if they are idle, unless {@code allowCoreThreadTimeOut} is set
     * @throws IllegalArgumentException if {@code corePoolSize < 0}
     */
    public ScheduledThreadPoolExecutor(int corePoolSize) {
        super(corePoolSize, Integer.MAX_VALUE,
              DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,
              new DelayedWorkQueue());
    }

SingleThreadPool通過Executors的newSignleThreadExecutor方法來創建。這類線程池內部只有一個核心線程,它確保所有的任務都在同一個線程中按順序執行。SingleThreadPool的意義在于統一所有的外界任務到一個線程中,這使得在這些任務之間不需要處理線程同步的問題。

   /**
     * Creates an Executor that uses a single worker thread operating
     * off an unbounded queue. (Note however that if this single
     * thread terminates due to a failure during execution prior to
     * shutdown, a new one will take its place if needed to execute
     * subsequent tasks.)  Tasks are guaranteed to execute
     * sequentially, and no more than one task will be active at any
     * given time. Unlike the otherwise equivalent
     * {@code newFixedThreadPool(1)} the returned executor is
     * guaranteed not to be reconfigurable to use additional threads.
     *
     * @return the newly created single-threaded Executor
     */
    public static ExecutorService newSingleThreadExecutor() {
        return new FinalizableDelegatedExecutorService
            (new ThreadPoolExecutor(1, 1,
                                    0L, TimeUnit.MILLISECONDS,
                                    new LinkedBlockingQueue<Runnable>()));
    }

官網中關于ThreadPoolExecutor的介紹

ps:文中的鏈接均為官網地址,如果打不開可以嘗試找個梯子。或者查看本地的幫助文檔也是一樣的。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,030評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,310評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,951評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,796評論 1 309
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,566評論 6 407
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,055評論 1 322
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,142評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,303評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,799評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,683評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,899評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,409評論 5 358
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,135評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,520評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,757評論 1 282
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,528評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,844評論 2 372

推薦閱讀更多精彩內容