Dalvik/ART(ANDROID)中的多线程机制(2)
Android消息處理機制(二)
角色綜述(回顧):
?? (1)UI thread通常就是main thread,而Android啟動程序時會替它建立一個MessageQueue。
(2)當然需要一個Looper對象,來管理該MessageQueue。
(3)我們可以構造Handler對象來push新消息到Message Queue里;或者接收Looper(從Message Queue取出)所送來的消息。
(4)線程A的Handler對象可以傳遞給別的線程,讓別的線程B或C等能送訊息來給線程A(存于A的Message Queue里)。
(5)線程A的Message Queue里的消息,只有線程A所屬的對象可以處理。
?
子線程傳遞消息給主線程
publicclass?Activity2extends?Activityimplements?OnClickListener{
?????? Buttonbutton?=null;
?????? TextViewtext?=null;
?????? MyHandlermHandler?=null;
?????? Threadthread?;
??????@Override
??????protectedvoid?onCreate(Bundle savedInstanceState) {
?????????????super.onCreate(savedInstanceState);
????????????? setContentView(R.layout.activity1);????????
?????????????button?= (Button)findViewById(R.id.btn);
?????????????button.setOnClickListener(this);
?????????????text?= (TextView)findViewById(R.id.content);
?????? }
??????publicvoid?onClick(View v) {
?????????????switch?(v.getId()) {
?????????????case?R.id.btn:
????????????????????thread?=new?MyThread();
????????????????????thread.start();
????????????????????break;
????????????? }????????????
?????? }?????
??????privateclass?MyHandlerextends?Handler{?????????????
?????????????public?MyHandler(Looper looper){
????????????????????super(looper);
????????????? }
?????????????@Override
?????????????publicvoid?handleMessage(Message msg) {//處理消息
????????????????????text.setText(msg.obj.toString());
????????????? }????????????
?????? }
??????privateclass?MyThreadextends?Thread{
?????????????@Override
?????????????publicvoid?run() {
???????????????????? Looper curLooper = Looper.myLooper();
???????????????????? Looper mainLooper = Looper.getMainLooper();
???????????????????? String msg ;
????????????????????if(curLooper==null){
???????????????????????????mHandler?=new?MyHandler(mainLooper);
??????????????????????????? msg ="curLooper is null";
???????????????????? }else{
???????????????????????????mHandler?=new?MyHandler(curLooper);
??????????????????????????? msg ="This is curLooper";
???????????????????? }
????????????????????mHandler.removeMessages(0);
???????????????????? Message m =mHandler.obtainMessage(1, 1, 1, msg);
????????????????????mHandler.sendMessage(m);
????????????? }????????????
?????? }
}
說明:
Android會自動替主線程建立Message Queue。在這個子線程里并沒有建立Message Queue。所以,myLooper值為null,而mainLooper則指向主線程里的Looper。于是,執行到:
mHandler = new MyHandler (mainLooper);
此mHandler屬于主線程。
?? mHandler.sendMessage(m);
就將m消息存入到主線程的Message Queue里。mainLooper看到Message Queue里有訊息,就會作出處理,于是由主線程執行到mHandler的handleMessage()來處理消息。
用Android線程間通信的Message機制
在Android下面也有多線程的概念,在C/C++中,子線程可以是一個函數,一般都是一個帶有循環的函數,來處理某些數據,優先線程只是一個復雜的運算過程,所以可能不需要while循環,運算完成,函數結束,線程就銷毀。對于那些需要控制的線程,一般我們都是和互斥鎖相互關聯,從而來控制線程的進度,一般我們創建子線程,一種線程是很常見的,那就是帶有消息循環的線程。
消息循環是一個很有用的線程方式,曾經自己用C在Linux下面實現一個消息循環的機制,往消息隊列里添加數據,然后異步的等待消息的返回。當消息隊列為空的時候就會掛起線程,等待新的消息的加入。這是一個很通用的機制。
在Android,這里的線程分為有消息循環的線程和沒有消息循環的線程,有消息循環的線程一般都會有一個Looper,這個事android的新概念。我們的主線程(UI線程)就是一個消息循環的線程。針對這種消息循環的機制,我們引入一個新的機制Handle,我們有消息循環,就要往消息循環里面發送相應的消息,自定義消息一般都會有自己對應的處理,消息的發送和清除,消息的的處理,把這些都封裝在Handle里面,注意Handle只是針對那些有Looper的線程,不管是UI線程還是子線程,只要你有Looper,我就可以往你的消息隊列里面添加東西,并做相應的處理。
但是這里還有一點,就是只要是關于UI相關的東西,就不能放在子線程中,因為子線程是不能操作UI的,只能進行數據、系統等其他非UI的操作。
那么什么情況下面我們的子線程才能看做是一個有Looper的線程呢?我們如何得到它Looper的句柄呢?
Looper.myLooper();獲得當前的Looper
Looper.getMainLooper () 獲得UI線程的Lopper
我們看看Handle的初始化函數,如果沒有參數,那么他就默認使用的是當前的Looper,如果有Looper參數,就是用對應的線程的Looper。
如果一個線程中調用Looper.prepare(),那么系統就會自動的為該線程建立一個消息隊列,然后調用 Looper.loop();之后就進入了消息循環,這個之后就可以發消息、取消息、和處理消息。這個如何發送消息和如何處理消息可以再其他的線程中通過Handle來做,但前提是我們的Hanle知道這個子線程的Looper,但是你如果不是在子線程運行 Looper.myLooper(),一般是得不到子線程的looper的。
public void run() {
? ?? ?? ?? ?synchronized (mLock) {
? ?? ?? ?? ?? ? Looper.prepare();
? ?? ?? ?? ?? ?//do something
? ?? ?? ?? ?}
? ?? ?? ?? ?Looper.loop();
? ?? ???}
所以很多人都是這樣做的:我直接在子線程中新建handle,然后在子線程中發送消息,這樣的話就失去了我們多線程的意義了。
class myThread extends Thread{
? ?? ?? ?? ? private EHandler mHandler ;
? ?? ?? ?? ? public void run() {
? ?? ?? ?? ?? ???Looper myLooper, mainLooper;
? ?? ?? ?? ?? ???myLooper = Looper.myLooper ();
? ?? ?? ?? ?? ? mainLooper = Looper.getMainLooper ();
? ?? ?? ?? ?? ? String obj;
? ?? ?? ?? ?? ? if (myLooper == null ){
? ?? ?? ?? ?? ?? ?? ?? ? mHandler = new EHandler(mainLooper);
? ?? ?? ?? ?? ?? ?? ?? ? obj = "current thread has no looper!" ;
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?? ? else {
? ?? ?? ?? ?? ?? ?? ?mHandler = new EHandler(myLooper);
? ?? ?? ?? ?? ?? ?? ?obj = "This is from current thread." ;
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?? ? mHandler .removeMessages(0);
? ?? ?? ?? ?? ? Message m = mHandler .obtainMessage(1, 1, 1, obj);
? ?? ?? ?? ?? ? mHandler .sendMessage(m);
? ?? ?? ?? ? }
??}
可以讓其他的線程來控制我們的handle,可以把 private EHandler mHandler ;放在外面,這樣我們的發消息和處理消息都可以在外面來定義,這樣增加程序代碼的美觀,結構更加清晰。
對如任何的Handle,里面必須要重載一個函數
public void handleMessage(Message msg)
這個函數就是我們的消息處理,如何處理,這里完全取決于你,然后通過 obtainMessage和 sendMessage等來生成和發送消息, removeMessages(0)來清除消息隊列。Google真是太智慧了,這種框架的產生,我們寫代碼更加輕松了。
有的時候,我們的子線程想去改變UI了,這個時候千萬不要再子線程中去修改,獲得UI線程的Looper,然后發送消息即可。
我們看看Goole Music App的源代碼。
在MediaPlaybackActivity.java中,我們可以看一下再OnCreate中的有這樣的兩句:
? ?? ???mAlbumArtWorker = new Worker("album art worker");
? ?? ???mAlbumArtHandler = new AlbumArtHandler(mAlbumArtWorker.getLooper());
很明顯這兩句,是構建了一個子線程。并且這個子線程還是Looper的子線程,這里很牛逼的使用了 mAlbumArtWorker.getLooper()這個函數,因為我們知道,我們能夠得到子線程的Looper的途徑只有一個:就是在子線程中調用 Looper.myLooper (),并且這個函數還要在我們perpare之后調用才能得到正確的Looper,但是他這里用了一個這樣的什么東東 getLooper,不知道它是如何實現的?
這里有一個大概的思路,我們在子線程的的prepare之后調用 myLooper ()這個方法,然后保存在一個成員變量中,這個getLooper就返回這個東西,但是這里會碰到多線程的一個很突出的問題,同步。我們在父線程中調用 mAlbumArtWorker.getLooper(),但是想要這個返回正確的looper就必須要求我們的子線程運行了prepare,但是這個東西實在子線程運行的,我們如何保證呢?
我們看Google是如何實現的?
? ?private class Worker implements Runnable {
? ?? ???private final Object mLock = new Object();
? ?? ???private Looper mLooper;
? ?? ???
? ?? ???/**
? ?? ?? ?* Creates a worker thread with the given name. The thread
? ?? ?? ?* then runs a [email=%7B@link]{@link[/email] android.os.Looper}.
? ?? ?? ?* @param name A name for the new thread
? ?? ?? ?*/
? ?? ???Worker(String name) {
? ?? ?? ?? ?Thread t = new Thread(null, this, name);
? ?? ?? ?? ?t.setPriority(Thread.MIN_PRIORITY);
? ?? ?? ?? ?t.start();
? ?? ?? ?? ?synchronized (mLock) {
? ?? ?? ?? ?? ? while (mLooper == null) {
? ?? ?? ?? ?? ?? ???try {
? ?? ?? ?? ?? ?? ?? ?? ?mLock.wait();
? ?? ?? ?? ?? ?? ???} catch (InterruptedException ex) {
? ?? ?? ?? ?? ?? ???}
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?}
? ?? ???}
? ?? ???
? ?? ???public Looper getLooper() {
? ?? ?? ?? ?return mLooper;
? ?? ???}
? ?? ???
? ?? ???public void run() {
? ?? ?? ?? ?synchronized (mLock) {
? ?? ?? ?? ?? ? Looper.prepare();
? ?? ?? ?? ?? ? mLooper = Looper.myLooper();
? ?? ?? ?? ?? ? mLock.notifyAll();
? ?? ?? ?? ?}
? ?? ?? ?? ?Looper.loop();
? ?? ???}
? ?? ???
? ?? ???public void quit() {
? ?? ?? ?? ?mLooper.quit();
? ?? ???}
? ? }
我們知道,一個線程類的構造函數是在主線程中完成的,所以在我們的 Worker的構造函數中我們創佳一個線程,然后讓這個線程運行,這一這個線程的創建是指定一個 Runnabl,這里就是我們的Worker本身,在主線程調用 t.start();,這后,我們子線程已經創建,并且開始執行work的run方法。然后下面的代碼很藝術:
synchronized (mLock) {
? ?? ?? ?? ?? ? while (mLooper == null) {
? ?? ?? ?? ?? ?? ???try {
? ?? ?? ?? ?? ?? ?? ?? ?mLock.wait();
? ?? ?? ?? ?? ?? ???} catch (InterruptedException ex) {
? ?? ?? ?? ?? ?? ???}
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?}
我們開始等待我們的子線程給mLooper賦值,如果不賦值我們就繼續等,然后我們的子線程在運行run方法之后,在給 mLooper賦值之后,通知worker夠著函數中的wait,然后我們的構造函數才能完成,所以我們說:
mAlbumArtWorker = new Worker("album art worker");
這句本身就是阻塞的,它創建了一個子線程,開啟了子線程,并且等待子線程給mLooper賦值,賦值完成之后,這個函數才返回,這樣才能保證我們的子線程的Looper的獲取絕對是正確的,這個構思很有創意。值得借鑒
Android中Handler的使用方法——在子線程中更新界面
本文主要介紹Android的Handler的使用方法。Handler可以發送Messsage和Runnable對象到與其相關聯的線程的消息隊列。每個Handler對象與創建它的線程相關聯,并且每個Handler對象只能與一個線程相關聯。
1.??? Handler一般有兩種用途:1)執行計劃任務,你可以再預定的實現執行某些任務,可以模擬定時器。2)線程間通信。在Android的應用啟動時,會創建一個主線程,主線程會創建一個消息隊列來處理各種消息。當你創建子線程時,你可以再你的子線程中拿到父線程中創建的Handler對象,就可以通過該對象向父線程的消息隊列發送消息了。由于Android要求在UI線程中更新界面,因此,可以通過該方法在其它線程中更新界面。
◆ 通過Runnable在子線程中更新界面的例子
1.○ 在onCreate中創建Handler?
public class HandlerTestApp extends Activity {?
??????? Handler mHandler;?
??????? TextView mText;?
??????? /** Called when the activity is first created. */?
?????? @Override?
?????? public void onCreate(Bundle savedInstanceState) {?
?????????? super.onCreate(savedInstanceState);?
?????????? setContentView(R.layout.main);?
?????????? mHandler = new Handler();//創建Handler?
?????????? mText = (TextView) findViewById(R.id.text0);//一個TextView?
?????? }?
???? ○ 構建Runnable對象,在runnable中更新界面,此處,我們修改了TextView的文字.此處需要說明的是,Runnable對象可以再主線程中創建,也可以再子線程中創建。我們此處是在子線程中創建的。?
???? Runnable mRunnable0 = new Runnable()?
??? {?
??????????????? @Override?
??????????????? public void run() {?
??????????????????????? mText.setText("This is Update from ohter thread, Mouse DOWN");
??????????????? }?
??? };?
???? ○ 創建子線程,在線程的run函數中,我們向主線程的消息隊列發送了一個runnable來更新界面。
??? private void updateUIByRunnable(){?
????????? new Thread()??
???????? {??
?????????????? //Message msg = mHandler.obtainMessage();??
????????????? public void run()??
???????????? {?
?????????????????? //mText.setText("This is Update from ohter thread, Mouse DOWN");//這句將拋出異常
???????????????????mHandler.post(mRunnable0);??
???????????? }??
???????? }.start();
???? }
◆ 用Message在子線程中來更新界面
1.??? 用Message更新界面與Runnable更新界面類似,只是需要修改幾個地方。
??? ○ 實現自己的Handler,對消息進行處理
??? private class?MyHandler?extends Handler
??? {?
??????? @Override?
??????? public void handleMessage(Message msg) {?
??????????? super.handleMessage(msg);?
??????????? switch(msg.what)?
??????????? {?
??????????? case?UPDATE://在收到消息時,對界面進行更新?
??????????????? mText.setText("This update by message");?
??????????????? break;?
??????????? }?
??????? }?
??? }
??? ○ 在新的線程中發送消息?????
??? private void updateByMessage()?
??? {?
??????? //匿名對象?
???????? new Thread()?
???????? {?
??????????????? public void run()?
??????????????? {?
??????????????????? //mText.setText("This is Update from ohter thread, Mouse DOWN");
??????????????????? //UPDATE是一個自己定義的整數,代表了消息ID?
??????????????????? Message msg = mHandler.obtainMessage(UPDATE);
????????????????????mHandler.sendMessage(msg);
??????????????? }?
???????? }.start();?
??? }
?
--------------------------------------------------------華麗的分割線-------------------------------------------------------------
?
?
android的消息處理有三個核心類:Looper,Handler和Message。其實還有一個Message Queue(消息隊列),但是MQ被封裝到Looper里面了,我們不會直接與MQ打交道,因此我沒將其作為核心類。下面一一介紹:
線程的魔法師 Looper
Looper的字面意思是“循環者”,它被設計用來使一個普通線程變成Looper線程。所謂Looper線程就是循環工作的線程。在程序開發中(尤其是GUI開發中),我們經常會需要一個線程不斷循環,一旦有新任務則執行,執行完繼續等待下一個任務,這就是Looper線程。使用Looper類創建Looper線程很簡單
| 1234567891011121314151617181920212223 | <pre?name="code" class="java">public class LooperThread extends Thread {?????@Override?public void run() {?????????// 將當前線程初始化為Looper線程??????????Looper.prepare();?????????// ...其他處理,如實例化handler??????????// 開始循環處理消息隊列??????????Looper.loop();?????}?}?<pre?name="code" class="java">public class LooperThread extends Thread {?@Overridepublic void run() {????????// 將當前線程初始化為Looper線程????????Looper.prepare();????????// ...其他處理,如實例化handler????????// 開始循環處理消息隊列????????Looper.loop();????}} |
通過上面兩行核心代碼,你的線程就升級為Looper線程了!!!是不是很神奇?讓我們放慢鏡頭,看看這兩行代碼各自做了什么。
1)Looper.prepare()
通過上圖可以看到,現在你的線程中有一個Looper對象,它的內部維護了一個消息隊列MQ。注意,一個Thread只能有一個Looper對象,為什么呢?咱們來看源碼。
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 | public?class?Looper {?????// 每個線程中的Looper對象其實是一個ThreadLocal,即線程本地存儲(TLS)對象??????private?static?final?ThreadLocal sThreadLocal =?new?ThreadLocal();????????// Looper內的消息隊列??????final?MessageQueue mQueue;????????// 當前線程??????Thread mThread;????????// 。。。其他屬性?????????// 每個Looper對象中有它的消息隊列,和它所屬的線程??????private?Looper() {?????????mQueue =?new?MessageQueue();?????????mRun =?true;?????????mThread = Thread.currentThread();?????}????????// 我們調用該方法會在調用線程的TLS中創建Looper對象??????public?static?final?void?prepare() {?????????if?(sThreadLocal.get() !=?null) {?????????????// 試圖在有Looper的線程中再次創建Looper將拋出異常??????????????throw?new?RuntimeException("Only one Looper may be created per thread");?????????}?????????sThreadLocal.set(new?Looper());?????}?????// 其他方法??}?public?class?Looper {????// 每個線程中的Looper對象其實是一個ThreadLocal,即線程本地存儲(TLS)對象????private?static?final?ThreadLocal sThreadLocal =?new?ThreadLocal();????// Looper內的消息隊列????final?MessageQueue mQueue;????// 當前線程????Thread mThread;????// 。。。其他屬性????// 每個Looper對象中有它的消息隊列,和它所屬的線程????private?Looper() {????????mQueue =?new?MessageQueue();????????mRun =?true;????????mThread = Thread.currentThread();????}????// 我們調用該方法會在調用線程的TLS中創建Looper對象????public?static?final?void?prepare() {????????if?(sThreadLocal.get() !=?null) {????????????// 試圖在有Looper的線程中再次創建Looper將拋出異常????????????throw?new?RuntimeException("Only one Looper may be created per thread");????????}????????sThreadLocal.set(new?Looper());????}????// 其他方法} |
通過源碼,prepare()背后的工作方式一目了然,其核心就是將looper對象定義為ThreadLocal。如果你還不清楚什么是ThreadLocal,請參考《理解ThreadLocal》。
2)Looper.loop()
調用loop方法后,Looper線程就開始真正工作了,它不斷從自己的MQ中取出隊頭的消息(也叫任務)執行。其源碼分析如下:
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 | public?static?final?void?loop() {?????????Looper me = myLooper();??//得到當前線程Looper??????????MessageQueue queue = me.mQueue;??//得到當前looper的MQ??????????// 這兩行沒看懂= = 不過不影響理解??????????Binder.clearCallingIdentity();????????????final?long?ident = Binder.clearCallingIdentity();????????????// 開始循環??????????while?(true) {?????????????Message msg = queue.next();?// 取出message??????????????if?(msg !=?null) {?????????????????if?(msg.target ==?null) {?????????????????????// message沒有target為結束信號,退出循環??????????????????????return;?????????????????}?????????????????// 日志。。。??????????????????if?(me.mLogging!=?null) me.mLogging.println(?????????????????????????">>>>> Dispatching to "?+ msg.target +?" "?????????????????????????+ msg.callback +?": "?+ msg.what?????????????????????????);?????????????????// 非常重要!將真正的處理工作交給message的target,即后面要講的handler??????????????????msg.target.dispatchMessage(msg);?????????????????// 還是日志。。。??????????????????if?(me.mLogging!=?null) me.mLogging.println(?????????????????????????"<<<<< Finished to??? "?+ msg.target +?" "?????????????????????????+ msg.callback);?????????????????????????????????// 下面沒看懂,同樣不影響理解??????????????????final?long?newIdent = Binder.clearCallingIdentity();?????????????????if?(ident != newIdent) {?????????????????????Log.wtf("Looper",?"Thread identity changed from 0x"?????????????????????????????+ Long.toHexString(ident) +?" to 0x"?????????????????????????????+ Long.toHexString(newIdent) +?" while dispatching to "?????????????????????????????+ msg.target.getClass().getName() +?" "+ msg.callback +?" what="?+ msg.what);?????????????????}?????????????????// 回收message資源??????????????????msg.recycle();?????????????}?????????}?????}?public?static?final?void?loop() {????????Looper me = myLooper();??//得到當前線程Looper????????MessageQueue queue = me.mQueue;??//得到當前looper的MQ????????// 這兩行沒看懂= = 不過不影響理解????????Binder.clearCallingIdentity();????????final?long?ident = Binder.clearCallingIdentity();????????// 開始循環????????while?(true) {????????????Message msg = queue.next();?// 取出message????????????if?(msg !=?null) {????????????????if?(msg.target ==?null) {????????????????????// message沒有target為結束信號,退出循環????????????????????return;????????????????}????????????????// 日志。。。????????????????if?(me.mLogging!=?null) me.mLogging.println(????????????????????????">>>>> Dispatching to "?+ msg.target +?" "????????????????????????+ msg.callback +?": "?+ msg.what????????????????????????);????????????????// 非常重要!將真正的處理工作交給message的target,即后面要講的handler????????????????msg.target.dispatchMessage(msg);????????????????// 還是日志。。。????????????????if?(me.mLogging!=?null) me.mLogging.println(????????????????????????"<<<<< Finished to??? "?+ msg.target +?" "????????????????????????+ msg.callback);????????????????????????????????// 下面沒看懂,同樣不影響理解????????????????final?long?newIdent = Binder.clearCallingIdentity();????????????????if?(ident != newIdent) {????????????????????Log.wtf("Looper",?"Thread identity changed from 0x"????????????????????????????+ Long.toHexString(ident) +?" to 0x"????????????????????????????+ Long.toHexString(newIdent) +?" while dispatching to "????????????????????????????+ msg.target.getClass().getName() +?" "+ msg.callback +?" what="?+ msg.what);????????????????}????????????????// 回收message資源????????????????msg.recycle();????????????}????????}????} |
除了prepare()和loop()方法,Looper類還提供了一些有用的方法,比如
Looper.myLooper()得到當前線程looper對象:
| 1234 | public?static?final?Looper myLooper() {?????????// 在任意線程調用Looper.myLooper()返回的都是那個線程的looper??????????return?(Looper)sThreadLocal.get();?}? |
getThread()得到looper對象所屬線程:
| 123 | public?Thread getThread() {?????????return?mThread;?}? |
quit()方法結束looper循環:
| 12345678910111213 | public?void?quit() {?????????// 創建一個空的message,它的target為NULL,表示結束循環消息??????????Message msg = Message.obtain();?????????// 發出消息??????????mQueue.enqueueMessage(msg,?0);?}?public?void?quit() {????????// 創建一個空的message,它的target為NULL,表示結束循環消息????????Message msg = Message.obtain();????????// 發出消息????????mQueue.enqueueMessage(msg,?0);} |
到此為止,你應該對Looper有了基本的了解,總結幾點:
1.每個線程有且最多只能有一個Looper對象,它是一個ThreadLocal
2.Looper內部有一個消息隊列,loop()方法調用后線程開始不斷從隊列中取出消息執行
3.Looper使一個線程變成Looper線程。
那么,我們如何往MQ上添加消息呢?下面有請Handler!(掌聲~~~)
異步處理大師 Handler
什么是handler?handler扮演了往MQ上添加消息和處理消息的角色(只處理由自己發出的消息),即通知MQ它要執行一個任務(sendMessage),并在loop到自己的時候執行該任務(handleMessage),整個過程是異步的。handler創建時會關聯一個looper,默認的構造方法將關聯當前線程的looper,不過這也是可以set的。默認的構造方法:
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 | public?class?handler {?????final?MessageQueue mQueue;??// 關聯的MQ?????????final?Looper mLooper;??// 關聯的looper?????????final?Callback mCallback;?????????// 其他屬性?????????public?Handler() {?????????// 沒看懂,直接略過,,,??????????if?(FIND_POTENTIAL_LEAKS) {?????????????final?Class<??extends?Handler> klass = getClass();?????????????if?((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&?????????????????????(klass.getModifiers() & Modifier.STATIC) ==?0) {?????????????????Log.w(TAG,?"The following Handler class should be static or leaks might occur: "?+?????????????????????klass.getCanonicalName());?????????????}?????????}?????????// 默認將關聯當前線程的looper??????????mLooper = Looper.myLooper();?????????// looper不能為空,即該默認的構造方法只能在looper線程中使用??????????if?(mLooper ==?null) {?????????????throw?new?RuntimeException(?????????????????"Can't create handler inside thread that has not called Looper.prepare()");?????????}?????????// 重要!!!直接把關聯looper的MQ作為自己的MQ,因此它的消息將發送到關聯looper的MQ上??????????mQueue = mLooper.mQueue;?????????mCallback =?null;?????}?????// 其他方法??}?public?class?handler {????final?MessageQueue mQueue;??// 關聯的MQ????final?Looper mLooper;??// 關聯的looper????final?Callback mCallback;????// 其他屬性????public?Handler() {????????// 沒看懂,直接略過,,,????????if?(FIND_POTENTIAL_LEAKS) {????????????final?Class<??extends?Handler> klass = getClass();????????????if?((klass.isAnonymousClass() || klass.isMemberClass() || klass.isLocalClass()) &&????????????????????(klass.getModifiers() & Modifier.STATIC) ==?0) {????????????????Log.w(TAG,?"The following Handler class should be static or leaks might occur: "?+????????????????????klass.getCanonicalName());????????????}????????}????????// 默認將關聯當前線程的looper????????mLooper = Looper.myLooper();????????// looper不能為空,即該默認的構造方法只能在looper線程中使用????????if?(mLooper ==?null) {????????????throw?new?RuntimeException(????????????????"Can't create handler inside thread that has not called Looper.prepare()");????????}????????// 重要!!!直接把關聯looper的MQ作為自己的MQ,因此它的消息將發送到關聯looper的MQ上????????mQueue = mLooper.mQueue;????????mCallback =?null;????}????// 其他方法} |
下面我們就可以為之前的LooperThread類加入Handler:
| 123456789101112131415161718192021222324252627282930313233 | public?class?LooperThread?extends?Thread {?????private?Handler handler1;????????private?Handler handler2;????????@Override?????public?void?run() {?????????// 將當前線程初始化為Looper線程??????????Looper.prepare();?????????// 實例化兩個handler??????????handler1 =?new?Handler();?????????handler2 =?new?Handler();?????????// 開始循環處理消息隊列??????????Looper.loop();?????}?}?public?class?LooperThread?extends?Thread {????private?Handler handler1;????private?Handler handler2;????@Override????public?void?run() {????????// 將當前線程初始化為Looper線程????????Looper.prepare();????????// 實例化兩個handler????????handler1 =?new?Handler();????????handler2 =?new?Handler();????????// 開始循環處理消息隊列????????Looper.loop();????}} |
加入handler后的效果如下圖:
可以看到,一個線程可以有多個Handler,但是只能有一個Looper!
Handler發送消息
有了handler之后,我們就可以使用?post(Runnable),postAtTime(Runnable, long),postDelayed(Runnable, long),sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long)和sendMessageDelayed(Message, long)這些方法向MQ上發送消息了。光看這些API你可能會覺得handler能發兩種消息,一種是Runnable對象,一種是message對象,這是直觀的理解,但其實post發出的Runnable對象最后都被封裝成message對象了,見源碼:
| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 | <pre name="code"?class="java"><span style="color:#000000;">public?final?boolean?post(Runnable r)?{?????// 注意getPostMessage(r)將runnable封裝成message??????return??sendMessageDelayed(getPostMessage(r),?0);?}????private?final?Message getPostMessage(Runnable r) {?????????Message m = Message.obtain();??//得到空的message???????m.callback = r;??//將runnable設為message的callback???????return?m;?}????public?boolean?sendMessageAtTime(Message msg,?long?uptimeMillis)?{??????boolean?sent =?false;??????MessageQueue queue = mQueue;??????if?(queue !=?null) {??????????msg.target =?this;??// message的target必須設為該handler!???????????sent = queue.enqueueMessage(msg, uptimeMillis);??????}??????else?{??????????RuntimeException e =?new?RuntimeException(??????????????this?+?" sendMessageAtTime() called with no mQueue");??????????Log.w("Looper", e.getMessage(), e);???????}???????return?sent;?}</span>?<pre name="code"?class="java"><span style="color:#000000;">public?final?boolean?post(Runnable r){????// 注意getPostMessage(r)將runnable封裝成message????return??sendMessageDelayed(getPostMessage(r),?0);}private?final?Message getPostMessage(Runnable r) {?????Message m = Message.obtain();??//得到空的message?????m.callback = r;??//將runnable設為message的callback?????return?m;}public?boolean?sendMessageAtTime(Message msg,?long?uptimeMillis){?????boolean?sent =?false;?????MessageQueue queue = mQueue;?????if?(queue !=?null) {?????????msg.target =?this;??// message的target必須設為該handler!?????????sent = queue.enqueueMessage(msg, uptimeMillis);?????}?????else?{?????????RuntimeException e =?new?RuntimeException(?????????????this?+?" sendMessageAtTime() called with no mQueue");?????????Log.w("Looper", e.getMessage(), e);??????}??????return?sent;}</span> |
其他方法就不羅列了,總之通過handler發出的message有如下特點:
1.message.target為該handler對象,這確保了looper執行到該message時能找到處理它的handler,即loop()方法中的關鍵代碼
msg.target.dispatchMessage(msg);2.post發出的message,其callback為Runnable對象
Handler處理消息
說完了消息的發送,再來看下handler如何處理消息。消息的處理是通過核心方法dispatchMessage(Message?msg)與鉤子方法handleMessage(Messagemsg)完成的,見源碼
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 | // 處理消息,該方法由looper調用??public?void?dispatchMessage(Message msg) {?????if?(msg.callback !=?null) {?????????// 如果message設置了callback,即runnable消息,處理callback!??????????handleCallback(msg);?????}?else?{?????????// 如果handler本身設置了callback,則執行callback??????????if?(mCallback !=?null) {??????????????/* 這種方法允許讓activity等來實現Handler.Callback接口,避免了自己編寫handler重寫handleMessage方法。見http://alex-yang-xiansoftware-com.iteye.com/blog/850865 */?????????????if?(mCallback.handleMessage(msg)) {?????????????????return;?????????????}?????????}?????????// 如果message沒有callback,則調用handler的鉤子方法handleMessage??????????handleMessage(msg);?????}?}????????// 處理runnable消息??private?final?void?handleCallback(Message message) {?????message.callback.run();??//直接調用run方法!??}????// 由子類實現的鉤子方法??public?void?handleMessage(Message msg) {?}?????// 處理消息,該方法由looper調用????public?void?dispatchMessage(Message msg) {????????if?(msg.callback !=?null) {????????????// 如果message設置了callback,即runnable消息,處理callback!????????????handleCallback(msg);????????}?else?{????????????// 如果handler本身設置了callback,則執行callback????????????if?(mCallback !=?null) {?????????????????/* 這種方法允許讓activity等來實現Handler.Callback接口,避免了自己編寫handler重寫handleMessage方法。見http://alex-yang-xiansoftware-com.iteye.com/blog/850865 */????????????????if?(mCallback.handleMessage(msg)) {????????????????????return;????????????????}????????????}????????????// 如果message沒有callback,則調用handler的鉤子方法handleMessage????????????handleMessage(msg);????????}????}????????// 處理runnable消息????private?final?void?handleCallback(Message message) {????????message.callback.run();??//直接調用run方法!????}????// 由子類實現的鉤子方法????public?void?handleMessage(Message msg) {????} |
可以看到,除了handleMessage(Message?msg)和Runnable對象的run方法由開發者實現外(實現具體邏輯),handler的內部工作機制對開發者是透明的。這正是handler API設計的精妙之處!
Handler的用處
我在小標題中將handler描述為“異步處理大師”,這歸功于Handler擁有下面兩個重要的特點:
1.handler可以在任意線程發送消息,這些消息會被添加到關聯的MQ上。
??????????????
2.handler是在它關聯的looper線程中處理消息的。
這就解決了android最經典的不能在其他非主線程中更新UI的問題。android的主線程也是一個looper線程(looper在android中運用很廣),我們在其中創建的handler默認將關聯主線程MQ。因此,利用handler的一個solution就是在activity中創建handler并將其引用傳遞給worker thread,worker thread執行完任務后使用handler發送消息通知activity更新UI。(過程如圖)
當然,handler能做的遠遠不僅如此,由于它能post Runnable對象,它還能與Looper配合實現經典的Pipeline Thread(流水線線程)模式。請參考此文《Android Guts: Intro to Loopers and Handlers》
總結
以上是生活随笔為你收集整理的Dalvik/ART(ANDROID)中的多线程机制(2)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jQuery插件——imgbox(点击图
- 下一篇: 几款不错的基于web 的sql编辑器