<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                Loop,Message,MessageQueue概念理解: Message:Handler發送、接收和處理的消息對象 Looper:每個線程只能擁有一個Looper.它的looper()方法負責循環讀取MessageQueue中的消息并將讀取到的消息交給發送該消息的handler進行處理。 MessageQueue:消息隊列,它采用先進先出的方式來管理Message。程序在創建Looper對象時,會在它的構造器中創建MessageQueue Looper提供的源碼如下: ~~~ private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread(); } ~~~ 從源碼第2行中可以看出,在創建Looper對象時會創建一個與之關聯的MessageQueue對象。構造器是private修飾的,所以程序員是無法創建Looper對象的,就是說**創建Looper同時就創建了MessageQueue對象** Handler:前面說Handler作用有兩個---發送消息和處理消息,Handler發送的消息必須被送到指定的MessageQueue,也就是說,要想Handler正常工作必須在當前線程中有一個MessageQueue,否則消息沒法保存。**而MessageQueue是由Looper負責管理的**,因此要想Handler正常工作,**必須在當前線程中有一個Looper對象**,這里分為兩種情況: 1>主線程(UI線程),系統已經初始化了一個Looper對象,因此程序直接創建Handler即可 2>程序員自己創建的子線程,這時,程序員必須創建一個Looper對象,并啟動它。 創建Looper:使用Looper.prepare(),查看源碼 ~~~ public static void prepare() { prepare(true); }    private static void prepare(boolean quitAllowed) { if (sThreadLocal.get() != null) { throw new RuntimeException("Only one Looper may be created per thread"); } sThreadLocal.set(new Looper(quitAllowed)); }   private Looper(boolean quitAllowed) { mQueue = new MessageQueue(quitAllowed); mThread = Thread.currentThread(); } ~~~ 通過方法調用,第9行創建Looper對象,創建Looper對象時同時會創建MessageQueue對象(第13行)。此外,可以看出prepare()允許一個線程最多有一個Looper被創建 啟動Looper:Looper.loop(),loop()使用一個死循環不斷取出MessageQueue中的消息,并將消息發送給對應的Handler進行處理。下面是Looper類中looper()方法的部分源碼 ~~~ for (;;) { Message msg = queue.next(); // might block if (msg == null) { // No message indicates that the message queue is quitting. return; } // This must be in a local variable, in case a UI event sets the logger Printer logging = me.mLogging; if (logging != null) { logging.println(">>>>> Dispatching to " + msg.target + " " + msg.callback + ": " + msg.what); } msg.target.dispatchMessage(msg); if (logging != null) { logging.println("<<<<< Finished to " + msg.target + " " + msg.callback); } // Make sure that during the course of dispatching the // identity of the thread wasn't corrupted. final long newIdent = Binder.clearCallingIdentity(); if (ident != newIdent) { Log.wtf(TAG, "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); } msg.recycleUnchecked(); } ~~~ 很明顯第1行用了一個死循環,第2行從queue中取出Message,第15行通過dispatchMessage(Message msg)方法將消息發送給Handler。 Looper,MessageQueue,Handler的各自作用如下: Looper:每個線程只有一個Looper,他負責管理MessageQueue,會不斷的從MessageQueue中取出消息,將消息交給對應的Handler處理 MessageQueue:由Looper負責管理,是用來存放線程放入的消息。 Handler:它把消息發送給Looper管理的MessageQueue,并負責處理Looper分給它的消息 在線程中Handler的使用步驟是: (1)調用 Looper的prepare()方法為當前線程創建Looper對象,創建Looper對象時,它的構造器會創建與之配套的MessageQueue。 (2)有了Looper之后,創建Handler子類的實例,重寫HandlerMessage()方法,該方法負責處理來自其它線程的消息。 (3)調用Looper的loop()方法啟動Looper。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看