Android的線程和線程池

線程分為主線程和子線程,主線程主要處理和界面相關的事情嗡靡,而子線程則往往用于執(zhí)行耗時操作讨彼。

AsyncTask封裝了線程池和Handler,它主要是為了方便開發(fā)者在子線程中更新UI哩至。HandlerThread是一種具有消息循環(huán)的線程蜜自,在它的內部可以使用Handler。IntentService是一個服務箭阶,系統(tǒng)對其進行了封裝使其可以更方便的執(zhí)行后臺任務仇参,IntentService內部采用HandlerThread來執(zhí)行任務,當任務執(zhí)行完畢后IntentService會自動退出待笑。從任務執(zhí)行的角度來看暮蹂,IntentService的作用很像一個后臺進程癌压,但是IntentService是一種服務,它不容易被系統(tǒng)殺死從而可以盡量保證任務的執(zhí)行集侯。

AsyncTask

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

AsyncTask是一個抽象的泛型類缅帘,它提供了Params难衰、Progress和Resul這三個泛型參數(shù)盖袭,其中Params表示參數(shù)的類型彼宠,Progress表示后臺任務的執(zhí)行進度的類型凭峡,而Result則表示后臺任務的返回結果的類型决记,如果AsyncTask缺失不需要傳遞具體的參數(shù)系宫,那么這三個泛型參數(shù)都可以使用Void來代替。AsyncTask的聲明如下椒惨。

public abstract class AsyncTask<Params, Progress, Result> 

AsyncTask提供了4個核心方法:

  1. onPreExecute()潮罪,在主線程中執(zhí)行,在異步任務執(zhí)行之前沃暗,此方法會被調用描睦,用于做準備工作导而。
  2. doInBackground(Params... params)今艺,在線程池中執(zhí)行虚缎,此方法用于執(zhí)行異步任務钓株,params參數(shù)表示異步任務的輸入?yún)?shù)轴合。在此方法中可以通過publishProgress方法來更新任務的進度,publishProgress方法會調用onProgressUpdate方法题涨。另外此方法需要返回計算結果給onPostExecute方法。
  3. onProgressUpdate(Progress... values)巡雨,在主線程中執(zhí)行铐望,當后臺任務的執(zhí)行進度發(fā)生改變時此方法會被調用茂附。
  4. onPostExecute(Result result)何之,在主線程中執(zhí)行,在異步任務執(zhí)行之后徊件,此方法會被調用蒜危,其中result參數(shù)是后臺任務的返回值辐赞,即doInBackground的返回值。

上面這幾個方法新思,onPreExecute先執(zhí)行夹囚,接著是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的對象必須在主線程中創(chuàng)建。
  3. execute方法不惜在UI線程調用苫费。
  4. 不要在程序中直接調用onPreExecute()百框、onPostExecute()牍汹、doInBackground 和 onProgressUpdate方法。
  5. 一個AsyncTask對象只能執(zhí)行一次嫁蛇,即只能調用一次execute方法睬棚,否則會報運行時異常解幼。
  6. 在Android1.6之前,AsyncTask是串行執(zhí)行任務的撵摆,Android1.6的時候AsyncTask開始采用線程池處理并行任務,在Android3.0開始台汇,為了避免AsyncTask所帶來的并發(fā)錯誤苟呐,AsyncTask又采用一個線程來串行執(zhí)行任務牵素,但可以通過AsyncTask的executeOnExecutor方法來并行的執(zhí)行任務笆呆。

官網(wǎng)上關于AsyncTask的說明

HandlerThread

HandlerThread繼承了Thread,它是一種可以使用Handler的Thread俄精,它的實現(xiàn)是在run方法中通過Looper.prepare()來創(chuàng)建消息隊列榕堰,并通過Looper.loop()來開啟消息循環(huán)逆屡,這樣在實際的使用中就允許在HandlerThread中創(chuàng)建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的實現(xiàn)來看砍的,它和普通的Thread有顯著的不同之處廓鞠。普通Thread主要用于在run方法中執(zhí)行一個耗時任務诫惭,而HandlerThread在內部創(chuàng)建了消息隊列蔓挖,外界需要通過Handler的消息方式來通知HandlerThread執(zhí)行一個具體的任務。由于HandlerThread的run方法是一個無限循環(huán)(loop是一個無限循環(huán)怨绣,run里面調用了Looper.loop()拷获,所以run也是無限循環(huán))匆瓜,因此當明確不需要再使用HandlerThread時,可以通過quit或者quitSafely方法來終止線程的執(zhí)行茧妒。

官網(wǎng)上關于HandlerThread的說明

IntentService

IntentService是一種特殊的Service桐筏,它繼承了Service并且它是一個抽象類拇砰,因此必須創(chuàng)建它的子類才能使用IntentService狰腌。IntentService可用于執(zhí)行后臺耗時的任務琼腔,當任務執(zhí)行后它會自動停止展姐,同時由于IntentService是服務剖毯,優(yōu)先級比單純的線程要高很多,所以更適合執(zhí)行一些高優(yōu)先級的后臺任務擂达。在實現(xiàn)上板鬓,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方法會創(chuàng)建一個HandlerThread,然后使用它的Looper來構造一個Handler對象mServiceHandler暂幼,這樣通過mServiceHandler發(fā)送的消息最終都會在HandlerThread中執(zhí)行旺嬉。IntentService是順序執(zhí)行后臺任務的起意。在Android8.0及以上版本揽咕,建議使用JobIntentService
官網(wǎng)上關于IntentService的說明

Android中的線程池

線程池的優(yōu)點:

  1. 重用線程池中的線程设易,避免弦音線程的創(chuàng)建和銷毀所帶來的性能開銷蛹头。
  2. 能有效控制線程池的最大并發(fā)數(shù)渣蜗,避免大量的線程之間因互相搶占系統(tǒng)資源而導致的阻塞現(xiàn)象。
  3. 能過對線程進行簡單的管理讼昆,并提供定時執(zhí)行以及指定間隔循環(huán)執(zhí)行等功能浸赫。

Android中的線程池的概念來源于Java中的Executor赃绊,Executor是一個接口,真正的線程池的實現(xiàn)為ThreadPoolExecutor运敢。 ThreadPoolExecutor提供了一系列參數(shù)來配置線程池者冤,通過不同的參數(shù)可以創(chuàng)建不同的線程池涉枫,從線程池的功能特性來說腐螟,Android的線程池主要分為4類,這4類線程池可以通過Executors所提供的工廠方法來得到衬廷。

ThreadPoolExecutor是線程池的真正實現(xiàn)吗跋,它的構造方法提供了一系列參數(shù)來配置線程池跌宛。它提供了4個構造方法,以下面這個來說明各個參數(shù)的含義蜕猫。

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

corePoolSize 線程池的核心線程數(shù)回右,默認情況下翔烁,核心線程會在線程池中一直存活,即使它們處于閑置狀態(tài)租漂。

maximumPoolSize線程池所能容納的最大線程數(shù)哩治,當活動線程數(shù)達到這個數(shù)值后衬鱼,后續(xù)的新任務將會被阻塞。

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

unit用于指定keepAliveTime參數(shù)的時間單位塘慕,是一個枚舉图呢,常用的有TimeUnit.MILLISECONDS骗随、TimeUnit.SECONDS蚊锹、TimeUnit.MINUTE等稚瘾。

workQueue線程池中的任務隊列摊欠,通過線程池的execute方法提交的Runable對象會存儲在這個參數(shù)中柱宦。

threadFactory線程工廠掸刊,為線程池提供創(chuàng)建新線程的功能忧侧。ThreadFactory是一個接口,它只有一個方法:Thread newThread(Runable r)蚓炬。

ThreadPoolExecutor執(zhí)行任務時大致遵循如下規(guī)則:

  1. 如果線程池中的線程數(shù)量未達到核心線程的數(shù)量肯夏,那么會直接啟動一個核心線程來執(zhí)行任務驯击。
  2. 如果線程池中的線程數(shù)量已經(jīng)達到或者超過核心線程的數(shù)量,那么任務會被插入到任務隊列中排隊等待執(zhí)行沪斟。
  3. 如果在步驟2中無法將任務插入到任務隊列中币喧,這往往死由于任務隊列已滿袱耽,這個時候如果線程數(shù)量未達到線程池規(guī)定的最大值朱巨,那么會立刻啟動給一個非核心線程來執(zhí)行任務。
  4. 如果步驟3中線程數(shù)量已經(jīng)達到線程池規(guī)定的最大值琼讽,那么就拒絕執(zhí)行任務,ThreadPoolExecutor會調用RejectedExecutionHandler的rejectedExecution方法來通知調用者吼蚁。

官網(wǎng)上關于ThreadPoolExecutor的說明

常見的線程池

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

FixedThreadPool 通過Executors的newFixedThredPool方法來創(chuàng)建注整。它是一種線程數(shù)量固定的線程池,當線程處于空閑狀態(tài)時肿轨,它們并不會被回收椒袍,除非線程池被關閉了。當所有的線程都處于活動狀態(tài)時曙蒸,新任務都會處于等待狀態(tài)岗钩,直到有線程空閑出來兼吓。由于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方法來創(chuàng)建暴拄。它是一種線程數(shù)量不定的線程池乖篷,它只有非核心線程撕蔼,并且其最大線程數(shù)為Integer.MAX_VALUE鲸沮。由于
Integer.MAX_VALUE是一個很大的數(shù)养距,實際上就相當于最大線程數(shù)可以任意大棍厌。當線程池中的線程都處于活動狀態(tài)時耘纱,線程池會創(chuàng)建新的線程來處理新任務毕荐,否則就會利用空閑的線程來處理新任務憎亚。線程池中的空閑線程都有超時機制,這個超時時長為60秒蝶锋,超過60秒限制線程就會被回收扳缕,CachedThreadPool的任務隊列相當于一個空集合别威,這將導致任何任務都會被立即執(zhí)行,因為在這種場景下SyncchornousQueue是無法插入任務的粥庄。比較適合執(zhí)行大量的耗時較少的任務飒赃。當整個線程池都處于閑置狀態(tài)時,線程池中的線程都會超時而被停職炒事,這個時候CachedThreadPool之中實際上是沒有任何線程的蔫慧,它幾乎是不占任何系統(tǒng)資源的姑躲。

    /**
     * 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方法來創(chuàng)建演顾。它的核心線程數(shù)量是固定的陈轿,而非核心線程數(shù)是沒有限制的,并且當非核心線程閑置時會被立即回收。ScheduledThreadPool這類線程池主要用于執(zhí)行定時任務和具有固定周期的重復任務的诵。

    /**
     * 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方法來創(chuàng)建。這類線程池內部只有一個核心線程驳糯,它確保所有的任務都在同一個線程中按順序執(zhí)行吼鳞。SingleThreadPool的意義在于統(tǒng)一所有的外界任務到一個線程中,這使得在這些任務之間不需要處理線程同步的問題蔼两。

   /**
     * 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>()));
    }

官網(wǎng)中關于ThreadPoolExecutor的介紹

ps:文中的鏈接均為官網(wǎng)地址甩鳄,如果打不開可以嘗試找個梯子《罨或者查看本地的幫助文檔也是一樣的妙啃。

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末抑胎,一起剝皮案震驚了整個濱河市突倍,隨后出現(xiàn)的幾起案子羽历,更是在濱河造成了極大的恐慌,老刑警劉巖疏尿,帶你破解...
    沈念sama閱讀 211,423評論 6 491
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡磕洪,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,147評論 2 385
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晰绎,“玉大人,你說我怎么就攤上這事」棺剩” “怎么了?”我有些...
    開封第一講書人閱讀 157,019評論 0 348
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我苟跪,道長元暴,這世上最難降的妖魔是什么昨寞? 我笑而不...
    開封第一講書人閱讀 56,443評論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上添瓷,老公的妹妹穿的比我還像新娘。我一直安慰自己搀愧,他們只是感情好,可當我...
    茶點故事閱讀 65,535評論 6 385
  • 文/花漫 我一把揭開白布处铛。 她就那樣靜靜地躺著奕塑,像睡著了一般。 火紅的嫁衣襯著肌膚如雪寝贡。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,798評論 1 290
  • 那天,我揣著相機與錄音鳖目,去河邊找鬼。 笑死狸捅,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播鞍匾,決...
    沈念sama閱讀 38,941評論 3 407
  • 文/蒼蘭香墨 我猛地睜開眼咆爽,長吁一口氣:“原來是場噩夢啊……” “哼斗埂!你這毒婦竟也來了?” 一聲冷哼從身側響起模闲,我...
    開封第一講書人閱讀 37,704評論 0 266
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎亮航,沒想到半個月后昼蛀,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,152評論 1 303
  • 正文 獨居荒郊野嶺守林人離奇死亡延欠,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 36,494評論 2 327
  • 正文 我和宋清朗相戀三年狞玛,在試婚紗的時候發(fā)現(xiàn)自己被綠了纠吴。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,629評論 1 340
  • 序言:一個原本活蹦亂跳的男人離奇死亡著淆,死狀恐怖,靈堂內的尸體忽然破棺而出懦砂,到底是詐尸還是另有隱情羽资,我是刑警寧澤,帶...
    沈念sama閱讀 34,295評論 4 329
  • 正文 年R本政府宣布脏答,位于F島的核電站辛孵,受9級特大地震影響,放射性物質發(fā)生泄漏习劫。R本人自食惡果不足惜谤狡,卻給世界環(huán)境...
    茶點故事閱讀 39,901評論 3 313
  • 文/蒙蒙 一捕仔、第九天 我趴在偏房一處隱蔽的房頂上張望斜做。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,742評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間津滞,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 31,978評論 1 266
  • 我被黑心中介騙來泰國打工隆敢, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留拂蝎,地道東北人悼泌。 一個月前我還...
    沈念sama閱讀 46,333評論 2 360
  • 正文 我出身青樓可柿,卻偏偏與公主長得像,于是被迫代替她去往敵國和親卵贱。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 43,499評論 2 348

推薦閱讀更多精彩內容