日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Dalvik/ART(ANDROID)中的多线程机制(2)

發(fā)布時間:2023/12/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Dalvik/ART(ANDROID)中的多线程机制(2) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Android消息處理機(jī)制(二)

角色綜述(回顧):

?? (1)UI thread通常就是main thread,而Android啟動程序時會替它建立一個MessageQueue。

(2)當(dāng)然需要一個Looper對象,來管理該MessageQueue。

(3)我們可以構(gòu)造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。于是,執(zhí)行到:

mHandler = new MyHandler (mainLooper);

mHandler屬于主線程。

?? mHandler.sendMessage(m);

就將m消息存入到主線程的Message Queue里。mainLooper看到Message Queue里有訊息,就會作出處理,于是由主線程執(zhí)行到mHandler的handleMessage()來處理消息。

用Android線程間通信的Message機(jī)制

在Android下面也有多線程的概念,在C/C++中,子線程可以是一個函數(shù),一般都是一個帶有循環(huán)的函數(shù),來處理某些數(shù)據(jù),優(yōu)先線程只是一個復(fù)雜的運算過程,所以可能不需要while循環(huán),運算完成,函數(shù)結(jié)束,線程就銷毀。對于那些需要控制的線程,一般我們都是和互斥鎖相互關(guān)聯(lián),從而來控制線程的進(jìn)度,一般我們創(chuàng)建子線程,一種線程是很常見的,那就是帶有消息循環(huán)的線程。
消息循環(huán)是一個很有用的線程方式,曾經(jīng)自己用C在Linux下面實現(xiàn)一個消息循環(huán)的機(jī)制,往消息隊列里添加數(shù)據(jù),然后異步的等待消息的返回。當(dāng)消息隊列為空的時候就會掛起線程,等待新的消息的加入。這是一個很通用的機(jī)制。
在Android,這里的線程分為有消息循環(huán)的線程和沒有消息循環(huán)的線程,有消息循環(huán)的線程一般都會有一個Looper,這個事android的新概念。我們的主線程(UI線程)就是一個消息循環(huán)的線程。針對這種消息循環(huán)的機(jī)制,我們引入一個新的機(jī)制Handle,我們有消息循環(huán),就要往消息循環(huán)里面發(fā)送相應(yīng)的消息,自定義消息一般都會有自己對應(yīng)的處理,消息的發(fā)送和清除,消息的的處理,把這些都封裝在Handle里面,注意Handle只是針對那些有Looper的線程,不管是UI線程還是子線程,只要你有Looper,我就可以往你的消息隊列里面添加?xùn)|西,并做相應(yīng)的處理。
但是這里還有一點,就是只要是關(guān)于UI相關(guān)的東西,就不能放在子線程中,因為子線程是不能操作UI的,只能進(jìn)行數(shù)據(jù)、系統(tǒng)等其他非UI的操作。
那么什么情況下面我們的子線程才能看做是一個有Looper的線程呢?我們?nèi)绾蔚玫剿麹ooper的句柄呢?
Looper.myLooper();獲得當(dāng)前的Looper
Looper.getMainLooper () 獲得UI線程的Lopper
我們看看Handle的初始化函數(shù),如果沒有參數(shù),那么他就默認(rèn)使用的是當(dāng)前的Looper,如果有Looper參數(shù),就是用對應(yīng)的線程的Looper。
如果一個線程中調(diào)用Looper.prepare(),那么系統(tǒng)就會自動的為該線程建立一個消息隊列,然后調(diào)用 Looper.loop();之后就進(jìn)入了消息循環(huán),這個之后就可以發(fā)消息、取消息、和處理消息。這個如何發(fā)送消息和如何處理消息可以再其他的線程中通過Handle來做,但前提是我們的Hanle知道這個子線程的Looper,但是你如果不是在子線程運行 Looper.myLooper(),一般是得不到子線程的looper的。
public void run() {
? ?? ?? ?? ?synchronized (mLock) {
? ?? ?? ?? ?? ? Looper.prepare();
? ?? ?? ?? ?? ?//do something
? ?? ?? ?? ?}
? ?? ?? ?? ?Looper.loop();
? ?? ???}
所以很多人都是這樣做的:我直接在子線程中新建handle,然后在子線程中發(fā)送消息,這樣的話就失去了我們多線程的意義了。
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 ;放在外面,這樣我們的發(fā)消息和處理消息都可以在外面來定義,這樣增加程序代碼的美觀,結(jié)構(gòu)更加清晰。
對如任何的Handle,里面必須要重載一個函數(shù)
public void handleMessage(Message msg)
這個函數(shù)就是我們的消息處理,如何處理,這里完全取決于你,然后通過 obtainMessage和 sendMessage等來生成和發(fā)送消息, removeMessages(0)來清除消息隊列。Google真是太智慧了,這種框架的產(chǎn)生,我們寫代碼更加輕松了。
有的時候,我們的子線程想去改變UI了,這個時候千萬不要再子線程中去修改,獲得UI線程的Looper,然后發(fā)送消息即可。
我們看看Goole Music App的源代碼。
在MediaPlaybackActivity.java中,我們可以看一下再OnCreate中的有這樣的兩句:
? ?? ???mAlbumArtWorker = new Worker("album art worker");
? ?? ???mAlbumArtHandler = new AlbumArtHandler(mAlbumArtWorker.getLooper());
很明顯這兩句,是構(gòu)建了一個子線程。并且這個子線程還是Looper的子線程,這里很牛逼的使用了 mAlbumArtWorker.getLooper()這個函數(shù),因為我們知道,我們能夠得到子線程的Looper的途徑只有一個:就是在子線程中調(diào)用 Looper.myLooper (),并且這個函數(shù)還要在我們perpare之后調(diào)用才能得到正確的Looper,但是他這里用了一個這樣的什么東東 getLooper,不知道它是如何實現(xiàn)的?
這里有一個大概的思路,我們在子線程的的prepare之后調(diào)用 myLooper ()這個方法,然后保存在一個成員變量中,這個getLooper就返回這個東西,但是這里會碰到多線程的一個很突出的問題,同步。我們在父線程中調(diào)用 mAlbumArtWorker.getLooper(),但是想要這個返回正確的looper就必須要求我們的子線程運行了prepare,但是這個東西實在子線程運行的,我們?nèi)绾伪WC呢?
我們看Google是如何實現(xiàn)的?
? ?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();
? ?? ???}
? ? }
我們知道,一個線程類的構(gòu)造函數(shù)是在主線程中完成的,所以在我們的 Worker的構(gòu)造函數(shù)中我們創(chuàng)佳一個線程,然后讓這個線程運行,這一這個線程的創(chuàng)建是指定一個 Runnabl,這里就是我們的Worker本身,在主線程調(diào)用 t.start();,這后,我們子線程已經(jīng)創(chuàng)建,并且開始執(zhí)行work的run方法。然后下面的代碼很藝術(shù):
synchronized (mLock) {
? ?? ?? ?? ?? ? while (mLooper == null) {
? ?? ?? ?? ?? ?? ???try {
? ?? ?? ?? ?? ?? ?? ?? ?mLock.wait();
? ?? ?? ?? ?? ?? ???} catch (InterruptedException ex) {
? ?? ?? ?? ?? ?? ???}
? ?? ?? ?? ?? ? }
? ?? ?? ?? ?}
我們開始等待我們的子線程給mLooper賦值,如果不賦值我們就繼續(xù)等,然后我們的子線程在運行run方法之后,在給 mLooper賦值之后,通知worker夠著函數(shù)中的wait,然后我們的構(gòu)造函數(shù)才能完成,所以我們說:
mAlbumArtWorker = new Worker("album art worker");
這句本身就是阻塞的,它創(chuàng)建了一個子線程,開啟了子線程,并且等待子線程給mLooper賦值,賦值完成之后,這個函數(shù)才返回,這樣才能保證我們的子線程的Looper的獲取絕對是正確的,這個構(gòu)思很有創(chuàng)意。值得借鑒

Android中Handler的使用方法——在子線程中更新界面

本文主要介紹Android的Handler的使用方法。Handler可以發(fā)送Messsage和Runnable對象到與其相關(guān)聯(lián)的線程的消息隊列。每個Handler對象與創(chuàng)建它的線程相關(guān)聯(lián),并且每個Handler對象只能與一個線程相關(guān)聯(lián)。

1.??? Handler一般有兩種用途:1)執(zhí)行計劃任務(wù),你可以再預(yù)定的實現(xiàn)執(zhí)行某些任務(wù),可以模擬定時器。2)線程間通信。在Android的應(yīng)用啟動時,會創(chuàng)建一個主線程,主線程會創(chuàng)建一個消息隊列來處理各種消息。當(dāng)你創(chuàng)建子線程時,你可以再你的子線程中拿到父線程中創(chuàng)建的Handler對象,就可以通過該對象向父線程的消息隊列發(fā)送消息了。由于Android要求在UI線程中更新界面,因此,可以通過該方法在其它線程中更新界面。
◆ 通過Runnable在子線程中更新界面的例子

1.○ 在onCreate中創(chuàng)建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();//創(chuàng)建Handler?
?????????? mText = (TextView) findViewById(R.id.text0);//一個TextView?
?????? }?
???? ○ 構(gòu)建Runnable對象,在runnable中更新界面,此處,我們修改了TextView的文字.此處需要說明的是,Runnable對象可以再主線程中創(chuàng)建,也可以再子線程中創(chuàng)建。我們此處是在子線程中創(chuàng)建的。?
???? Runnable mRunnable0 = new Runnable()?
??? {?
??????????????? @Override?
??????????????? public void run() {?
??????????????????????? mText.setText("This is Update from ohter thread, Mouse DOWN");
??????????????? }?
??? };?
???? ○ 創(chuàng)建子線程,在線程的run函數(shù)中,我們向主線程的消息隊列發(fā)送了一個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更新界面類似,只是需要修改幾個地方。
??? ○ 實現(xiàn)自己的Handler,對消息進(jìn)行處理

??? private class?MyHandler?extends Handler
??? {?

??????? @Override?
??????? public void handleMessage(Message msg) {?
??????????? super.handleMessage(msg);?
??????????? switch(msg.what)?
??????????? {?
??????????? case?UPDATE://在收到消息時,對界面進(jìn)行更新?
??????????????? mText.setText("This update by message");?
??????????????? break;?
??????????? }?
??????? }?
??? }

??? ○ 在新的線程中發(fā)送消息?????
??? private void updateByMessage()?
??? {?
??????? //匿名對象?
???????? new Thread()?
???????? {?
??????????????? public void run()?
??????????????? {?
??????????????????? //mText.setText("This is Update from ohter thread, Mouse DOWN");

??????????????????? //UPDATE是一個自己定義的整數(shù),代表了消息ID?
??????????????????? Message msg = mHandler.obtainMessage(UPDATE);
????????????????????mHandler.sendMessage(msg);
??????????????? }?
???????? }.start();?
??? }

?

--------------------------------------------------------華麗的分割線-------------------------------------------------------------

?

?

android的消息處理有三個核心類:Looper,Handler和Message。其實還有一個Message Queue(消息隊列),但是MQ被封裝到Looper里面了,我們不會直接與MQ打交道,因此我沒將其作為核心類。下面一一介紹:

線程的魔法師 Looper

Looper的字面意思是“循環(huán)者”,它被設(shè)計用來使一個普通線程變成Looper線程。所謂Looper線程就是循環(huán)工作的線程。在程序開發(fā)中(尤其是GUI開發(fā)中),我們經(jīng)常會需要一個線程不斷循環(huán),一旦有新任務(wù)則執(zhí)行,執(zhí)行完繼續(xù)等待下一個任務(wù),這就是Looper線程。使用Looper類創(chuàng)建Looper線程很簡單

1234567891011121314151617181920212223<pre?name="code" class="java">public class LooperThread extends Thread {?????@Override?public void run() {?????????// 將當(dāng)前線程初始化為Looper線程??????????Looper.prepare();?????????// ...其他處理,如實例化handler??????????// 開始循環(huán)處理消息隊列??????????Looper.loop();?????}?}?<pre?name="code" class="java">public class LooperThread extends Thread {?@Overridepublic void run() {????????// 將當(dāng)前線程初始化為Looper線程????????Looper.prepare();????????// ...其他處理,如實例化handler????????// 開始循環(huán)處理消息隊列????????Looper.loop();????}}

  

通過上面兩行核心代碼,你的線程就升級為Looper線程了!!!是不是很神奇?讓我們放慢鏡頭,看看這兩行代碼各自做了什么。

1)Looper.prepare()

通過上圖可以看到,現(xiàn)在你的線程中有一個Looper對象,它的內(nèi)部維護(hù)了一個消息隊列MQ。注意,一個Thread只能有一個Looper對象,為什么呢?咱們來看源碼。

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859public?class?Looper {?????// 每個線程中的Looper對象其實是一個ThreadLocal,即線程本地存儲(TLS)對象??????private?static?final?ThreadLocal sThreadLocal =?new?ThreadLocal();????????// Looper內(nèi)的消息隊列??????final?MessageQueue mQueue;????????// 當(dāng)前線程??????Thread mThread;????????// 。。。其他屬性?????????// 每個Looper對象中有它的消息隊列,和它所屬的線程??????private?Looper() {?????????mQueue =?new?MessageQueue();?????????mRun =?true;?????????mThread = Thread.currentThread();?????}????????// 我們調(diào)用該方法會在調(diào)用線程的TLS中創(chuàng)建Looper對象??????public?static?final?void?prepare() {?????????if?(sThreadLocal.get() !=?null) {?????????????// 試圖在有Looper的線程中再次創(chuàng)建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內(nèi)的消息隊列????final?MessageQueue mQueue;????// 當(dāng)前線程????Thread mThread;????// 。。。其他屬性????// 每個Looper對象中有它的消息隊列,和它所屬的線程????private?Looper() {????????mQueue =?new?MessageQueue();????????mRun =?true;????????mThread = Thread.currentThread();????}????// 我們調(diào)用該方法會在調(diào)用線程的TLS中創(chuàng)建Looper對象????public?static?final?void?prepare() {????????if?(sThreadLocal.get() !=?null) {????????????// 試圖在有Looper的線程中再次創(chuàng)建Looper將拋出異常????????????throw?new?RuntimeException("Only one Looper may be created per thread");????????}????????sThreadLocal.set(new?Looper());????}????// 其他方法}

  

通過源碼,prepare()背后的工作方式一目了然,其核心就是將looper對象定義為ThreadLocal。如果你還不清楚什么是ThreadLocal,請參考《理解ThreadLocal》。

2)Looper.loop()

調(diào)用loop方法后,Looper線程就開始真正工作了,它不斷從自己的MQ中取出隊頭的消息(也叫任務(wù))執(zhí)行。其源碼分析如下:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081public?static?final?void?loop() {?????????Looper me = myLooper();??//得到當(dāng)前線程Looper??????????MessageQueue queue = me.mQueue;??//得到當(dāng)前l(fā)ooper的MQ??????????// 這兩行沒看懂= = 不過不影響理解??????????Binder.clearCallingIdentity();????????????final?long?ident = Binder.clearCallingIdentity();????????????// 開始循環(huán)??????????while?(true) {?????????????Message msg = queue.next();?// 取出message??????????????if?(msg !=?null) {?????????????????if?(msg.target ==?null) {?????????????????????// message沒有target為結(jié)束信號,退出循環(huán)??????????????????????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();??//得到當(dāng)前線程Looper????????MessageQueue queue = me.mQueue;??//得到當(dāng)前l(fā)ooper的MQ????????// 這兩行沒看懂= = 不過不影響理解????????Binder.clearCallingIdentity();????????final?long?ident = Binder.clearCallingIdentity();????????// 開始循環(huán)????????while?(true) {????????????Message msg = queue.next();?// 取出message????????????if?(msg !=?null) {????????????????if?(msg.target ==?null) {????????????????????// message沒有target為結(jié)束信號,退出循環(huán)????????????????????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()得到當(dāng)前線程looper對象:

1234public?static?final?Looper myLooper() {?????????// 在任意線程調(diào)用Looper.myLooper()返回的都是那個線程的looper??????????return?(Looper)sThreadLocal.get();?}?

  getThread()得到looper對象所屬線程:

123public?Thread getThread() {?????????return?mThread;?}?

  quit()方法結(jié)束looper循環(huán):

12345678910111213public?void?quit() {?????????// 創(chuàng)建一個空的message,它的target為NULL,表示結(jié)束循環(huán)消息??????????Message msg = Message.obtain();?????????// 發(fā)出消息??????????mQueue.enqueueMessage(msg,?0);?}?public?void?quit() {????????// 創(chuàng)建一個空的message,它的target為NULL,表示結(jié)束循環(huán)消息????????Message msg = Message.obtain();????????// 發(fā)出消息????????mQueue.enqueueMessage(msg,?0);}

  

到此為止,你應(yīng)該對Looper有了基本的了解,總結(jié)幾點:

1.每個線程有且最多只能有一個Looper對象,它是一個ThreadLocal

2.Looper內(nèi)部有一個消息隊列,loop()方法調(diào)用后線程開始不斷從隊列中取出消息執(zhí)行

3.Looper使一個線程變成Looper線程。

那么,我們?nèi)绾瓮鵐Q上添加消息呢?下面有請Handler!(掌聲~~~)

異步處理大師 Handler

什么是handler?handler扮演了往MQ上添加消息和處理消息的角色(只處理由自己發(fā)出的消息),即通知MQ它要執(zhí)行一個任務(wù)(sendMessage),并在loop到自己的時候執(zhí)行該任務(wù)(handleMessage),整個過程是異步的。handler創(chuàng)建時會關(guān)聯(lián)一個looper,默認(rèn)的構(gòu)造方法將關(guān)聯(lián)當(dāng)前線程的looper,不過這也是可以set的。默認(rèn)的構(gòu)造方法:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465public?class?handler {?????final?MessageQueue mQueue;??// 關(guān)聯(lián)的MQ?????????final?Looper mLooper;??// 關(guān)聯(lián)的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());?????????????}?????????}?????????// 默認(rèn)將關(guān)聯(lián)當(dāng)前線程的looper??????????mLooper = Looper.myLooper();?????????// looper不能為空,即該默認(rèn)的構(gòu)造方法只能在looper線程中使用??????????if?(mLooper ==?null) {?????????????throw?new?RuntimeException(?????????????????"Can't create handler inside thread that has not called Looper.prepare()");?????????}?????????// 重要!!!直接把關(guān)聯(lián)looper的MQ作為自己的MQ,因此它的消息將發(fā)送到關(guān)聯(lián)looper的MQ上??????????mQueue = mLooper.mQueue;?????????mCallback =?null;?????}?????// 其他方法??}?public?class?handler {????final?MessageQueue mQueue;??// 關(guān)聯(lián)的MQ????final?Looper mLooper;??// 關(guān)聯(lián)的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());????????????}????????}????????// 默認(rèn)將關(guān)聯(lián)當(dāng)前線程的looper????????mLooper = Looper.myLooper();????????// looper不能為空,即該默認(rèn)的構(gòu)造方法只能在looper線程中使用????????if?(mLooper ==?null) {????????????throw?new?RuntimeException(????????????????"Can't create handler inside thread that has not called Looper.prepare()");????????}????????// 重要!!!直接把關(guān)聯(lián)looper的MQ作為自己的MQ,因此它的消息將發(fā)送到關(guān)聯(lián)looper的MQ上????????mQueue = mLooper.mQueue;????????mCallback =?null;????}????// 其他方法}

  下面我們就可以為之前的LooperThread類加入Handler:

123456789101112131415161718192021222324252627282930313233public?class?LooperThread?extends?Thread {?????private?Handler handler1;????????private?Handler handler2;????????@Override?????public?void?run() {?????????// 將當(dāng)前線程初始化為Looper線程??????????Looper.prepare();?????????// 實例化兩個handler??????????handler1 =?new?Handler();?????????handler2 =?new?Handler();?????????// 開始循環(huán)處理消息隊列??????????Looper.loop();?????}?}?public?class?LooperThread?extends?Thread {????private?Handler handler1;????private?Handler handler2;????@Override????public?void?run() {????????// 將當(dāng)前線程初始化為Looper線程????????Looper.prepare();????????// 實例化兩個handler????????handler1 =?new?Handler();????????handler2 =?new?Handler();????????// 開始循環(huán)處理消息隊列????????Looper.loop();????}}

  

加入handler后的效果如下圖:

可以看到,一個線程可以有多個Handler,但是只能有一個Looper!

Handler發(fā)送消息

有了handler之后,我們就可以使用?post(Runnable),postAtTime(Runnable, long),postDelayed(Runnable, long),sendEmptyMessage(int),sendMessage(Message),sendMessageAtTime(Message, long)和sendMessageDelayed(Message, long)這些方法向MQ上發(fā)送消息了。光看這些API你可能會覺得handler能發(fā)兩種消息,一種是Runnable對象,一種是message對象,這是直觀的理解,但其實post發(fā)出的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設(shè)為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必須設(shè)為該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設(shè)為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必須設(shè)為該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發(fā)出的message有如下特點:

1.message.target為該handler對象,這確保了looper執(zhí)行到該message時能找到處理它的handler,即loop()方法中的關(guān)鍵代碼

msg.target.dispatchMessage(msg);

2.post發(fā)出的message,其callback為Runnable對象

Handler處理消息

說完了消息的發(fā)送,再來看下handler如何處理消息。消息的處理是通過核心方法dispatchMessage(Message?msg)與鉤子方法handleMessage(Messagemsg)完成的,見源碼

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253// 處理消息,該方法由looper調(diào)用??public?void?dispatchMessage(Message msg) {?????if?(msg.callback !=?null) {?????????// 如果message設(shè)置了callback,即runnable消息,處理callback!??????????handleCallback(msg);?????}?else?{?????????// 如果handler本身設(shè)置了callback,則執(zhí)行callback??????????if?(mCallback !=?null) {??????????????/* 這種方法允許讓activity等來實現(xiàn)Handler.Callback接口,避免了自己編寫handler重寫handleMessage方法。見http://alex-yang-xiansoftware-com.iteye.com/blog/850865 */?????????????if?(mCallback.handleMessage(msg)) {?????????????????return;?????????????}?????????}?????????// 如果message沒有callback,則調(diào)用handler的鉤子方法handleMessage??????????handleMessage(msg);?????}?}????????// 處理runnable消息??private?final?void?handleCallback(Message message) {?????message.callback.run();??//直接調(diào)用run方法!??}????// 由子類實現(xiàn)的鉤子方法??public?void?handleMessage(Message msg) {?}?????// 處理消息,該方法由looper調(diào)用????public?void?dispatchMessage(Message msg) {????????if?(msg.callback !=?null) {????????????// 如果message設(shè)置了callback,即runnable消息,處理callback!????????????handleCallback(msg);????????}?else?{????????????// 如果handler本身設(shè)置了callback,則執(zhí)行callback????????????if?(mCallback !=?null) {?????????????????/* 這種方法允許讓activity等來實現(xiàn)Handler.Callback接口,避免了自己編寫handler重寫handleMessage方法。見http://alex-yang-xiansoftware-com.iteye.com/blog/850865 */????????????????if?(mCallback.handleMessage(msg)) {????????????????????return;????????????????}????????????}????????????// 如果message沒有callback,則調(diào)用handler的鉤子方法handleMessage????????????handleMessage(msg);????????}????}????????// 處理runnable消息????private?final?void?handleCallback(Message message) {????????message.callback.run();??//直接調(diào)用run方法!????}????// 由子類實現(xiàn)的鉤子方法????public?void?handleMessage(Message msg) {????}

  

可以看到,除了handleMessage(Message?msg)和Runnable對象的run方法由開發(fā)者實現(xiàn)外(實現(xiàn)具體邏輯),handler的內(nèi)部工作機(jī)制對開發(fā)者是透明的。這正是handler API設(shè)計的精妙之處!

Handler的用處

我在小標(biāo)題中將handler描述為“異步處理大師”,這歸功于Handler擁有下面兩個重要的特點:

1.handler可以在任意線程發(fā)送消息,這些消息會被添加到關(guān)聯(lián)的MQ上。

??????????????

2.handler是在它關(guān)聯(lián)的looper線程中處理消息的。

這就解決了android最經(jīng)典的不能在其他非主線程中更新UI的問題。android的主線程也是一個looper線程(looper在android中運用很廣),我們在其中創(chuàng)建的handler默認(rèn)將關(guān)聯(lián)主線程MQ。因此,利用handler的一個solution就是在activity中創(chuàng)建handler并將其引用傳遞給worker thread,worker thread執(zhí)行完任務(wù)后使用handler發(fā)送消息通知activity更新UI。(過程如圖)

當(dāng)然,handler能做的遠(yuǎn)遠(yuǎn)不僅如此,由于它能post Runnable對象,它還能與Looper配合實現(xiàn)經(jīng)典的Pipeline Thread(流水線線程)模式。請參考此文《Android Guts: Intro to Loopers and Handlers》


總結(jié)

以上是生活随笔為你收集整理的Dalvik/ART(ANDROID)中的多线程机制(2)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。