Android的線程和線程池

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

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

AsyncTask

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

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

public abstract class AsyncTask<Params, Progress, Result> 

AsyncTask提供了4個核心方法:

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

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

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線程調(diào)用。
  4. 不要在程序中直接調(diào)用onPreExecute()、onPostExecute()、doInBackground 和 onProgressUpdate方法。
  5. 一個AsyncTask對象只能執(zhí)行一次,即只能調(diào)用一次execute方法,否則會報運行時異常。
  6. 在Android1.6之前,AsyncTask是串行執(zhí)行任務(wù)的,Android1.6的時候AsyncTask開始采用線程池處理并行任務(wù),在Android3.0開始,為了避免AsyncTask所帶來的并發(fā)錯誤,AsyncTask又采用一個線程來串行執(zhí)行任務(wù),但可以通過AsyncTask的executeOnExecutor方法來并行的執(zhí)行任務(wù)。

官網(wǎng)上關(guān)于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í)行一個耗時任務(wù),而HandlerThread在內(nèi)部創(chuàng)建了消息隊列,外界需要通過Handler的消息方式來通知HandlerThread執(zhí)行一個具體的任務(wù)。由于HandlerThread的run方法是一個無限循環(huán)(loop是一個無限循環(huán),run里面調(diào)用了Looper.loop(),所以run也是無限循環(huán)),因此當明確不需要再使用HandlerThread時,可以通過quit或者quitSafely方法來終止線程的執(zhí)行。

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

IntentService

IntentService是一種特殊的Service,它繼承了Service并且它是一個抽象類,因此必須創(chuàng)建它的子類才能使用IntentService。IntentService可用于執(zhí)行后臺耗時的任務(wù),當任務(wù)執(zhí)行后它會自動停止,同時由于IntentService是服務(wù),優(yōu)先級比單純的線程要高很多,所以更適合執(zhí)行一些高優(yōu)先級的后臺任務(wù)。在實現(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方法會被調(diào)用,onCreate方法會創(chuàng)建一個HandlerThread,然后使用它的Looper來構(gòu)造一個Handler對象mServiceHandler,這樣通過mServiceHandler發(fā)送的消息最終都會在HandlerThread中執(zhí)行。IntentService是順序執(zhí)行后臺任務(wù)的。在Android8.0及以上版本,建議使用JobIntentService。
官網(wǎng)上關(guān)于IntentService的說明

Android中的線程池

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

  1. 重用線程池中的線程,避免弦音線程的創(chuàng)建和銷毀所帶來的性能開銷。
  2. 能有效控制線程池的最大并發(fā)數(shù),避免大量的線程之間因互相搶占系統(tǒng)資源而導(dǎo)致的阻塞現(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),它的構(gòu)造方法提供了一系列參數(shù)來配置線程池。它提供了4個構(gòu)造方法,以下面這個來說明各個參數(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ù)的新任務(wù)將會被阻塞。

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

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

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

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

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

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

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

常見的線程池

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

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

    /**
     * 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只有核心線程并且這些核心線程沒有超時機制,另外任務(wù)隊列也沒有大小限制。

CachedThreadPool通過Executors的newCachedThreadPool方法來創(chuàng)建。它是一種線程數(shù)量不定的線程池,它只有非核心線程,并且其最大線程數(shù)為Integer.MAX_VALUE。由于
Integer.MAX_VALUE是一個很大的數(shù),實際上就相當于最大線程數(shù)可以任意大。當線程池中的線程都處于活動狀態(tài)時,線程池會創(chuàng)建新的線程來處理新任務(wù),否則就會利用空閑的線程來處理新任務(wù)。線程池中的空閑線程都有超時機制,這個超時時長為60秒,超過60秒限制線程就會被回收,CachedThreadPool的任務(wù)隊列相當于一個空集合,這將導(dǎo)致任何任務(wù)都會被立即執(zhí)行,因為在這種場景下SyncchornousQueue是無法插入任務(wù)的。比較適合執(zhí)行大量的耗時較少的任務(wù)。當整個線程池都處于閑置狀態(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í)行定時任務(wù)和具有固定周期的重復(fù)任務(wù)

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

   /**
     * 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)中關(guān)于ThreadPoolExecutor的介紹

ps:文中的鏈接均為官網(wǎng)地址,如果打不開可以嘗試找個梯子?;蛘卟榭幢镜氐膸椭臋n也是一樣的。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容