Android線程之自定義帶消息循環(huán)Looper的實(shí)例

Android 線程之自定義帶消息循環(huán)Looper的實(shí)例

創(chuàng)新互聯(lián)建站主營且末網(wǎng)站建設(shè)的網(wǎng)絡(luò)公司,主營網(wǎng)站建設(shè)方案,成都APP應(yīng)用開發(fā),且末h5小程序開發(fā)搭建,且末網(wǎng)站營銷推廣歡迎且末等地區(qū)企業(yè)咨詢

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到底是怎么工作的。

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)的。

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

名稱欄目:Android線程之自定義帶消息循環(huán)Looper的實(shí)例
本文URL:http://muchs.cn/article4/gdehoe.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供定制網(wǎng)站小程序開發(fā)、用戶體驗(yàn)云服務(wù)器、外貿(mào)網(wǎng)站建設(shè)網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站