線程分為主線程和子線程,主線程主要處理和界面相關的事情,而子線程則往往用于執行耗時操作。
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個核心方法:
- onPreExecute(),在主線程中執行,在異步任務執行之前,此方法會被調用,用于做準備工作。
- doInBackground(Params... params),在線程池中執行,此方法用于執行異步任務,params參數表示異步任務的輸入參數。在此方法中可以通過publishProgress方法來更新任務的進度,publishProgress方法會調用onProgressUpdate方法。另外此方法需要返回計算結果給onPostExecute方法。
- onProgressUpdate(Progress... values),在主線程中執行,當后臺任務的執行進度發生改變時此方法會被調用。
- 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在具體的使用過程中有如下需要注意的地方:
- AsyncTask的類必須在主線程中加載。
- AsyncTask的對象必須在主線程中創建。
- execute方法不惜在UI線程調用。
- 不要在程序中直接調用onPreExecute()、onPostExecute()、doInBackground 和 onProgressUpdate方法。
- 一個AsyncTask對象只能執行一次,即只能調用一次execute方法,否則會報運行時異常。
- 在Android1.6之前,AsyncTask是串行執行任務的,Android1.6的時候AsyncTask開始采用線程池處理并行任務,在Android3.0開始,為了避免AsyncTask所帶來的并發錯誤,AsyncTask又采用一個線程來串行執行任務,但可以通過AsyncTask的executeOnExecutor方法來并行的執行任務。
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方法來終止線程的執行。
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中的線程池
線程池的優點:
- 重用線程池中的線程,避免弦音線程的創建和銷毀所帶來的性能開銷。
- 能有效控制線程池的最大并發數,避免大量的線程之間因互相搶占系統資源而導致的阻塞現象。
- 能過對線程進行簡單的管理,并提供定時執行以及指定間隔循環執行等功能。
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執行任務時大致遵循如下規則:
- 如果線程池中的線程數量未達到核心線程的數量,那么會直接啟動一個核心線程來執行任務。
- 如果線程池中的線程數量已經達到或者超過核心線程的數量,那么任務會被插入到任務隊列中排隊等待執行。
- 如果在步驟2中無法將任務插入到任務隊列中,這往往死由于任務隊列已滿,這個時候如果線程數量未達到線程池規定的最大值,那么會立刻啟動給一個非核心線程來執行任務。
- 如果步驟3中線程數量已經達到線程池規定的最大值,那么就拒絕執行任務,ThreadPoolExecutor會調用RejectedExecutionHandler的rejectedExecution方法來通知調用者。
常見的線程池
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>()));
}
ps:文中的鏈接均為官網地址,如果打不開可以嘗試找個梯子。或者查看本地的幫助文檔也是一樣的。