分析Android中線程和線程池
由于內容過多,所以將分為上下兩部分,第一部分主要和大家談談Android中的線程,以及在Android中的常用的線程池。第二部分我們一起來了解一下AsyncTask的使用和工作原理。
HandlerThreadHandlerThread是Thread的子類,它是一種可以使用Handler的Thread,它的實現比較簡單。我們來看看它的源碼:
package android.os;public class HandlerThread extends Thread { int mPriority; int mTid = -1; Looper mLooper; public HandlerThread(String name) {super(name);mPriority = Process.THREAD_PRIORITY_DEFAULT; } public HandlerThread(String name, int priority) {super(name);mPriority = priority; } protected void onLooperPrepared() { } @Override public void run() {mTid = Process.myTid();Looper.prepare();synchronized (this) { mLooper = Looper.myLooper(); notifyAll();}Process.setThreadPriority(mPriority);onLooperPrepared();Looper.loop();mTid = -1; } public Looper getLooper() {if (!isAlive()) { return null;}// If the thread has been started, wait until the looper has been created.synchronized (this) { while (isAlive() && mLooper == null) {try { wait();} catch (InterruptedException e) {} }}return mLooper; } public boolean quit() {Looper looper = getLooper();if (looper != null) { looper.quit(); return true;}return false; } public boolean quitSafely() {Looper looper = getLooper();if (looper != null) { looper.quitSafely(); return true;}return false; } public int getThreadId() {return mTid; }}
為了讓大家看清楚,我們源碼的一些英文注釋干掉了,現在就很清晰了。整個類中,除了構造方法和對外提供幾個public方法以外,就剩一個方法了run()。從它的實現來看,和普通的Thread實現沒有什么區別。都是在run()方法中執行耗時操作。不過,HandlerThread內部創建了消息隊列,并且run()方法是一個無限循環的方法,當我們不需要HandlerThread的時候,我們可以調用quitSafely()或者quit()方法來結束這個線程。這是比較方便的。
IntentServiceIntentService是一種特殊的Service,它是Service的子類,并且它是一個抽象類,所以必須創建它的子類才可以使用Intent Service。Intent Service可用于執行后臺的耗時任務,當任務執行完畢,它會自己結束,不需要開發著手動結束它。這里需要注意一個問題,Intentservice內置有線程,但是它還是屬于Service,所以它的優先級會比線程高很多,所以不容易被系統殺死。所以比較合適去執行一些優先級比較高的任務??纯此脑创a:
package android.app;import android.annotation.WorkerThread;import android.annotation.Nullable;import android.content.Intent;import android.os.Handler;import android.os.HandlerThread;import android.os.IBinder;import android.os.Looper;import android.os.Message;public abstract class IntentService extends Service { private volatile Looper mServiceLooper; private volatile ServiceHandler mServiceHandler; private String mName; private boolean mRedelivery; private final class ServiceHandler extends Handler {public ServiceHandler(Looper looper) { super(looper);}@Overridepublic void handleMessage(Message msg) { onHandleIntent((Intent)msg.obj); stopSelf(msg.arg1);} } public IntentService(String name) {super();mName = name; } public void setIntentRedelivery(boolean enabled) {mRedelivery = enabled; } @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); } @Override public void onStart(@Nullable Intent intent, int startId) {Message msg = mServiceHandler.obtainMessage();msg.arg1 = startId;msg.obj = intent;mServiceHandler.sendMessage(msg); } @Override public int onStartCommand(@Nullable Intent intent, int flags, int startId) {onStart(intent, startId);return mRedelivery ? START_REDELIVER_INTENT : START_NOT_STICKY; } @Override public void onDestroy() {mServiceLooper.quit(); } public IBinder onBind(Intent intent) {return null; } @WorkerThread protected abstract void onHandleIntent(@Nullable Intent intent);}
這里就很簡單了,這些方法對于經常使用Service的朋友來說,就很熟悉了。大家看onCreate()方法。沒錯IntentService就是封裝了HandlerThread和Handler。
當我們啟動IntentService是onCreate(),方法將會被調用,然后就會創建HandlerThread和ServiceHandler。而onStartCommand()方法又調用了onStart()方法,從onStart()方法可以看出IntentService 僅僅是通過ServiceHandler來發一個消息,這個消息會在HandlerThread中被處理掉。
大家看這個onStart()方法,將intent作為消息傳遞給onHandleIntent,這個intent通常是我們傳遞進來的數據。而onHandleIntent就是通過這個intent來區別具體的后臺任務的。
好了,AsyncTask的使用和工作原理。我們會在下一章在說。下面我們看看線程池吧。
不知道大家有沒有遇到過這種情況。我們在寫項目,遇到耗時操作的時候,怎么辦呢,是不是new Thread().start,那這樣的話,整個項目中得new多少個Thread。這種明顯是很浪費性能。畢竟線程也是好資源的嘛。那么有沒有一種可以方法對線程進行復用呢?答案就是線程池。
線程池的好處1、重用線程池中的線程,避免因為線程的創建和銷毀帶來的性能開銷。
2、能有效的控制線程池中的線程并發數,避免大量線程之間因為互相搶占資源而導致的阻塞現象。
3、能夠對線程進行簡單的管理,并提供定時執行以及指定間隔循環執行等功能。
ThreadPoolExecutorAndroid中的線程池概念是來源于java中Executor,Executor是一個空的接口,真正的線程池實現ThreadPoolExecutor。
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory) {this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, defaultHandler); }
簡單介紹一下ThreadPoolExcutor各個參數的含義
corePoolSize:線程池的核心線程數,默認情況下,核心線程會在線程池中一直存活,即使他們處于閑置狀態。當我們把ThreadPoolExecutor中的allowCoreThreadTimeOut屬性設置為true,那么閑置的核心線程在等待新任務的時候,如果時間超過keepAliveTime所設置的時間,核心線程將會被回收。
maximumPoolSize:設置最大線程池能夠容納的最大線程數,當線程池中的線程達到這個數以后,新任務將會被阻塞。
keepAliveTime:非核心線程數閑置的時間。
unit:指定keepAliveTime參數的時間單位。
workQueue:線程池中的任務隊列。
threadFactory:線程工廠,為線程池提供創建新線程的功能。
線程池的分類Android中常見的線程池有四種,FixedThreadPool、CachedThreadPool、ScheduledThreadPool、SingleThreadExecutor。
FixedThreadPoolFixedThreadPool線程池是通過Executors的new FixedThreadPool方法來創建。它的特點是該線程池中的線程數量是固定的。即使線程處于閑置的狀態,它們也不會被回收,除非線程池被關閉。當所有的線程都處于活躍狀態的時候,新任務就處于隊列中等待線程來處理。注意,FixedThreadPool只有核心線程,沒有非核心線程。
public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) { return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);}CachedThreadPool
CachedThreadPool線程池是通過Executors的newCachedThreadPool進行創建的。它是一種線程數目不固定的線程池,它沒有核心線程,只有非核心線程,當線程池中的線程都處于活躍狀態,就會創建新的線程來處理新的任務。否則就會利用閑置的線程來處理新的任務。線程池中的線程都有超時機制,這個超時機制時長是60s,超過這個時間,閑置的線程就會被回收。這種線程池適合處理大量并且耗時較少的任務。這里得說一下,CachedThreadPool的任務隊列,基本都是空的。
public static ExecutorService newCachedThreadPool() { return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());}ScheduledThreadPool
ScheduledThreadPool線程池是通過Executors的newScheduledThreadPool進行創建的,它的核心線程是固定的,但是非核心線程數是不固定的,并且當非核心線程一處于空閑狀態,就立即被回收。這種線程適合執行定時任務和具有固定周期的重復任務。
public static ScheduledExecutorService newScheduledThreadPool( int corePoolSize, ThreadFactory threadFactory) {return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory); }public ScheduledThreadPoolExecutor(int corePoolSize, ThreadFactory threadFactory) {super(corePoolSize, Integer.MAX_VALUE, DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS, new DelayedWorkQueue(), threadFactory); }SingleThreadExecutor
SingleThreadExecutor線程池是通過Executors的newSingleThreadExecutor方法來創建的,這類線程池中只有一個核心線程,也沒有非核心線程,這就確保了所有任務能夠在同一個線程并且按照順序來執行,這樣就不需要考慮線程同步的問題。
public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService (new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>())); }
以上就是分析Android中線程和線程池的詳細內容,更多關于Android中線程和線程池的資料請關注好吧啦網其它相關文章!
相關文章: