上一篇文章提到了Android系統(tǒng)的UI線程是一種帶消息循環(huán)(Looper)機(jī)制的線程,同時(shí)Android也提供了封裝有消息循環(huán)(Looper)的HandlerThread類,這種線程,可以綁定Handler()對象,并通過Handler的sendMessage()函數(shù)向線程發(fā)送消息,通過handleMessage()函數(shù),處理線程接收到的消息。這么說比較抽象,那么,本文就利用基礎(chǔ)的Java類庫,實(shí)現(xiàn)一個(gè)帶消息循環(huán)(Looper)的線程,以幫助初學(xué)者理解這樣一個(gè)Looper到底是怎么工作的。

創(chuàng)新互聯(lián)是一家專業(yè)的成都網(wǎng)站建設(shè)公司,我們專注網(wǎng)站設(shè)計(jì)制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營銷、企業(yè)網(wǎng)站建設(shè),友情鏈接,1元廣告為企業(yè)客戶提供一站式建站解決方案,能帶給客戶新的互聯(lián)網(wǎng)理念。從網(wǎng)站結(jié)構(gòu)的規(guī)劃UI設(shè)計(jì)到用戶體驗(yàn)提高,創(chuàng)新互聯(lián)力求做到盡善盡美。
1. 首先,我們完成一個(gè)簡單的線程框架。
public class LooperThread {
private volatile boolean mIsLooperQuit = false;
private Thread mThread;
public void start() {
if( mThread != null ) {
return;
}
mIsLooperQuit = false;
mThread = new Thread(mLooperRunnable);
mThread.start();
}
public void stop() {
if( mThread == null ) {
return;
}
mIsLooperQuit = true;
mThread = null;
}
protected Runnable mLooperRunnable = new Runnable() {
@Override
public void run() {
while( !mIsLooperQuit ) {
}
}
};
}如上述代碼所示,mLooperRunnable.run()循環(huán)執(zhí)行線程任務(wù),mIsLooperQuit則是線程退出循環(huán)的條件。下面,我們將添加消息的發(fā)送和處理代碼。
2. 添加線程循環(huán)的消息發(fā)送和處理代碼
(1) 定義消息結(jié)構(gòu)體,創(chuàng)建消息隊(duì)列
public class LooperThread {
private Queue<Message> mMessageQueue = new LinkedList<Message>();
public static class Message {
int what;
}
}(2) 創(chuàng)建互斥鎖和條件變量
public class LooperThread {
private Lock mLock = new ReentrantLock();
private Condition mCondition = mLock.newCondition();
}(3) 創(chuàng)建發(fā)送消息的函數(shù)
//發(fā)送消息,由外部其他模塊調(diào)用,發(fā)送消息給線程
public void sendMessage( Message message ) {
if( mThread == null ) {
return;
}
mLock.lock();
mMessageQueue.add(message); //添加消息到消息隊(duì)列
mCondition.signal(); //通知線程循環(huán),有消息來了,請立即處理
mLock.unlock();
}(4) 創(chuàng)建處理消息的函數(shù)
//處理消息,由線程內(nèi)部調(diào)用
public void handleMessage(Message message) {
//這里可以通過一個(gè)Callback來回調(diào)監(jiān)聽者
}(5) 在mLooperRunnable.run()循環(huán)中解析消息
protected Runnable mLooperRunnable = new Runnable() {
@Override
public void run() {
while( !mIsLooperQuit ) {
mLock.lock();
Message message = null;
try {
while( !mIsLooperQuit && mMessageQueue.isEmpty() ) {
mCondition.await(); //沒有消息到來則休眠
}
message = mMessageQueue.poll();
}
catch (InterruptedException e) {
e.printStackTrace();
}
finally {
mLock.unlock();
}
handleMessage(message );
}
};
}(6) 修改線程的Stop()函數(shù),喚醒休眠的消息循環(huán)
public void stop() {
if( mThread == null ) {
return;
}
mIsLooperQuit = true;
mLock.lock();
mCondition.signal();
mLock.unlock();
mMessageQueue.clear();
mThread = null;
}到這里,一個(gè)基本的帶有消息循環(huán)的線程類封裝就完成了,相信大家應(yīng)該從編寫這段代碼的過程中,理解了系統(tǒng)是如何實(shí)現(xiàn)消息循環(huán)的。完整的代碼見博文最后的附件,有任何疑問歡迎留言或者來信[email protected]交流,或者關(guān)注我的新浪微博 @盧_俊 獲取最新的文章和資訊。
當(dāng)前標(biāo)題:Android開發(fā)實(shí)踐:自定義帶消息循環(huán)(Looper)的工作線程
分享URL:http://www.jbt999.com/article8/iipiip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)站排名、App開發(fā)、商城網(wǎng)站、網(wǎng)站營銷、企業(yè)建站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:[email protected]。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)