Android消息处理机制源码分析

  一, 消息处理机制,主要涉及到类有HandlerThread、Handler、Looper、MessageQueue、Message     
    1. Message在这里面代表一个消息实体对象
    2. MessageQueue主要用来存放消息实体的队列
    3. Looper主要用来监听MessageQueue的消息,他存放于ThreadLocal中
    4. Handler主要用来处理消息的发送,以及响应消息的业务处理
    5. HandlerThread是一个单独的线程,可与Looper整合,实现一个完全独立的消息处理机制
 二,对应类方法的UML图
 Android消息处理机制源码分析
 三,对应类的具体实现描述
    1. HandlerThead
      操作整个消息机制的demo,
       //创建一个新的线程,在创建线程时,已经初始化了Looper
          HandlerThread handlerThread= new HandlerThread("looper test" );
          handlerThread.start();
          
          Handler handler= new Handler(handlerThread.getLooper()){
               @Override
               public void handleMessage(Message msg) {
                   
                   Log. d("loop mes exec", "loop mes exec");
              }
          };
          handler.sendMessage(Message. obtain());
          handler.sendMessage(Message. obtain());
          handler.sendMessage(Message. obtain());
          handler.sendMessage(Message. obtain());
          handler.sendMessage(Message. obtain());
          
           try {
              Thread. sleep(50000L);
          } catch (InterruptedException e) {
              e.printStackTrace();
          }
          
           //自己创建的也要关掉
          handlerThread.quit();
2,  Looper(相对于线程独立,因为是放在ThreadLocal存放)

 

   a)Looper中提供的prepareMainLooper()专门为UI主线程提供,在平常方法调用时不应该用       此方法,可查看ActivityThread#main()源码
     b)在使用Looper处理消息机制时,首先通过Looper.perpare()创建一个Looper对象,并将他存           储 在对应的sThreadLocal之中.
    
 /** Initialize the current thread as a looper.
      * This gives you a chance to create handlers that then reference
      * this looper, before actually starting the loop. Be sure to call
      * {@link #loop()} after calling this method, and end it by calling
      * {@link #quit()}.
      */
    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));
    }
   c)然后通过Looper.loop()进行循环监听,等待MessageQueuee队列里面的消息
 public static void loop () {
        final Looper me = myLooper();
        if (me == null) {
            throw new RuntimeException( "No Looper; Looper.prepare() wasn't called on this thread.");
        }
        final MessageQueue queue = me. mQueue;

        // Make sure the identity of this thread is that of the local process,
        // and keep track of what that identity token actually is.
        Binder.clearCallingIdentity();
        final long ident = Binder. clearCallingIdentity();

        for (;;) {
            //查看下面对MessageQueue的详解,
            Message msg = queue.next(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }
            //对消息进行处理
            msg.target.dispatchMessage(msg);
            //消息回收
            msg.recycle();
        }
    }
 3, Handler
   a)Handler以sendMessage(Message msg)发送消息,最终会把消息扔入        MessageQueue
   
  private boolean enqueueMessage (MessageQueue queue, Message msg, long uptimeMillis) {
        msg. target = this;
        if (mAsynchronous) {
            msg.setAsynchronous( true);
        }
        return queue.enqueueMessage(msg, uptimeMillis);
 }
 --->MessageQueue.enqueueMessage(...),把消息扔给队列
   b)看Looper中的msg.target.dispatchMessage(msg)对得到的消息进行处理
    4, MessageQueue
       这个类中主要的两个方法一个是发送消息时处理的方法enqueueMessae(...)一个是            Looper.loop()中处理消息时处理的next()
  
public class MessageQueue {
    // True if the message queue can be quit.
    private final boolean mQuitAllowed;

    @SuppressWarnings( "unused")
    private int mPtr; // used by native code

//消息是以链状形式存储,这个是顶级消息
    Message mMessages;
    //见名思义,主要是用于next()方法中,空闲时处理
    private final ArrayList<IdleHandler> mIdleHandlers = new ArrayList<IdleHandler>();
    private IdleHandler[] mPendingIdleHandlers;
    private boolean mQuiting;

    // 在next()方法中对是否阻塞判断,当有消息在处理时mBlocked=false
    private boolean mBlocked;

    // The next barrier token.
    // Barriers are indicated by messages with a null target whose arg1 field carries the token.
    private int mNextBarrierToken;

    private native void nativeInit();
    private native void nativeDestroy();
    //看所传参数,应该是用于消息阻塞等待的时间
    private native void nativePollOnce( int ptr, int timeoutMillis);
    //与上面成对,对上面的阻塞进行唤醒
    private native void nativeWake( int ptr);

    /**
     * Callback interface for discovering when a thread is going to block
     * waiting for more messages.
     */
    public static interface IdleHandler {
        /**
         * Called when the message queue has run out of messages and will now
         * wait for more.  Return true to keep your idle handler active, false
         * to have it removed.  This may be called if there are still messages
         * pending in the queue, but they are all scheduled to be dispatched
         * after the current time.
         */
        boolean queueIdle();
    }
    //.....

    MessageQueue(boolean quitAllowed) {
        mQuitAllowed = quitAllowed;
        nativeInit();
    }

    @Override
    protected void finalize() throws Throwable {
        try {
            nativeDestroy();
        } finally {
            super.finalize();
        }
    }

    //***************************Looper.loop()调用此方法***********************
    final Message next() {
        int pendingIdleHandlerCount = -1; // -1 only during first iteration
        int nextPollTimeoutMillis = 0;

        for (;;) {
            if (nextPollTimeoutMillis != 0) {
                Binder.flushPendingCommands();
            }
            //由下面代码可看出,主要有两点会进入阻塞状态,一是当消息队列中没有消息时,它会使线程进入等待状态
            //二是消息队列中有消息,但是消息指定了执行的时间,而现在还没有到这个时间,线程也会进入等待状态
            nativePollOnce( mPtr, nextPollTimeoutMillis);

            synchronized ( this) {
                if ( mQuiting) {
                    return null;
                }

                // Try to retrieve the next message.  Return if found.
                final long now = SystemClock.uptimeMillis();
                Message prevMsg = null;
                Message msg = mMessages;
                if (msg != null && msg. target == null) {
                    // Stalled by a barrier.  Find the next asynchronous message in the queue.
                    //从消息队列中检索出消息,
                    do {
                        prevMsg = msg;
                        msg = msg. next;
                    } while (msg != null && !msg.isAsynchronous());
                }
                if (msg != null) {
                    if (now < msg. when) {
                        // Next message is not ready.  Set a timeout to wake up when it is ready.
                        // 计算时间,计算阻塞等待的时间,一般可能会疑问上面的nativePollOnce在怎么时候执行,阻塞在
                        // 怎么时候被使用,主要是在执行下面逻辑操作时,可能有其他消息进入这个方法,时间在这时候起作用
                        nextPollTimeoutMillis = ( int) Math.min(msg.when - now, Integer. MAX_VALUE);
                    } else {
                        // Got a message.
                        //在此对阻塞的开关设值,代表有值在处理,在enqueueMessage中对这个开关进行处理
                        mBlocked = false;
                        if (prevMsg != null) {
                            prevMsg. next = msg. next;
                        } else {
                            mMessages = msg. next;
                        }
                        msg. next = null;
                        if ( false) Log.v( "MessageQueue", "Returning message: " + msg);
                        //标记为已使用
                        msg.markInUse();
                        return msg;
                    }
                } else {
                    // No more messages.
                    nextPollTimeoutMillis = -1;
                }

                // If first time idle, then get the number of idlers to run.
                // Idle handles only run if the queue is empty or if the first message
                // in the queue (possibly a barrier) is due to be handled in the future.
                if (pendingIdleHandlerCount < 0
                        && ( mMessages == null || now < mMessages.when )) {
                    pendingIdleHandlerCount = mIdleHandlers.size();
                }
                if (pendingIdleHandlerCount <= 0) {
                    // No idle handlers to run.  Loop and wait some more.
                    // 标识为空闲,可以处理其他消息
                    mBlocked = true;
                    continue;
                }

                if ( mPendingIdleHandlers == null) {
                    mPendingIdleHandlers = new IdleHandler[Math.max(pendingIdleHandlerCount, 4)];
                }
                mPendingIdleHandlers = mIdleHandlers.toArray(mPendingIdleHandlers );
            }

            // Run the idle handlers.
            // We only ever reach this code block during the first iteration.
            for ( int i = 0; i < pendingIdleHandlerCount; i++) {
                final IdleHandler idler = mPendingIdleHandlers[i];
                mPendingIdleHandlers[i] = null; // release the reference to the handler
 
                //处理idleHandler当返回true时,执行完删除,返回fasle不删除
                boolean keep = false;
                try {
                    keep = idler.queueIdle();
                } catch (Throwable t) {
                    Log. wtf("MessageQueue", "IdleHandler threw exception", t);
                }

                if (!keep) {
                    synchronized ( this) {
                        mIdleHandlers.remove(idler);
                    }
                }
            }

            // Reset the idle handler count to 0 so we do not run them again.
            pendingIdleHandlerCount = 0;

            // While calling an idle handler, a new message could have been delivered
            // so go back and look again for a pending message without waiting.
            // 标0代表不用再阻塞等待,可以继续执行
            nextPollTimeoutMillis = 0;
        }
    }

    final void quit() {
        if (!mQuitAllowed) {
            throw new RuntimeException( "Main thread not allowed to quit.");
        }

        synchronized ( this) {
            if ( mQuiting) {
                return;
            }
            mQuiting = true;
        }
        nativeWake( mPtr);
    }

    //目前没有看到应用常景,代表逻辑主要是生成一个具体时间的消息,并对arg1赋token值
    final int enqueueSyncBarrier( long when) {
        // Enqueue a new sync barrier token.
        // We don't need to wake the queue because the purpose of a barrier is to stall it.
        synchronized ( this) {
            final int token = mNextBarrierToken++;
            final Message msg = Message. obtain();
            msg. arg1 = token;

            Message prev = null;
            Message p = mMessages;
            if (when != 0) {
                while (p != null && p. when <= when) {
                    prev = p;
                    p = p. next;
                }
            }
            if (prev != null) { // invariant: p == prev.next
                msg. next = p;
                prev. next = msg;
            } else {
                msg. next = p;
                mMessages = msg;
            }
            return token;
        }
    }

    final void removeSyncBarrier( int token) {
        // Remove a sync barrier token from the queue.
        // If the queue is no longer stalled by a barrier then wake it.
        final boolean needWake;
        synchronized ( this) {
            Message prev = null;
            Message p = mMessages;
            while (p != null && (p. target != null || p.arg1 != token)) {
                prev = p;
                p = p. next;
            }
            if (p == null) {
                throw new IllegalStateException( "The specified message queue synchronization "
                        + " barrier token has not been posted or has already been removed.");
            }
            if (prev != null) {
                prev. next = p. next;
                needWake = false;
            } else {
                mMessages = p. next;
                needWake = mMessages == null || mMessages.target != null;
            }
            p.recycle();
        }
        if (needWake) {
            nativeWake( mPtr);
        }
    }

   //**************************Handler.sendMessage调用此方法***********************
    final boolean enqueueMessage(Message msg, long when) {
        if (msg.isInUse()) {
            throw new AndroidRuntimeException(msg + " This message is already in use.");
        }
        if (msg.target == null) {
            throw new AndroidRuntimeException( "Message must have a target.");
        }

        boolean needWake;
        synchronized ( this) {
            if ( mQuiting) {
                RuntimeException e = new RuntimeException(
                        msg. target + " sending message to a Handler on a dead thread");
                Log. w("MessageQueue", e.getMessage(), e);
                return false;
            }

            msg. when = when;
            Message p = mMessages;
            //Message的整个队列是以when的时间升序排列的,但时间比顶级消息还要小时,直接把消息加入第一个
            if (p == null || when == 0 || when < p. when) {
                // New head, wake up the event queue if blocked.
                msg. next = p;
                mMessages = msg;
                needWake = mBlocked;
            } else {
                // Inserted within the middle of the queue.  Usually we don't have to wake
                // up the event queue unless there is a barrier at the head of the queue
                // and the message is the earliest asynchronous message in the queue.
                // 判断当前msg所处在链状中的位置,对这异步消息一直不怎么理解
                needWake = mBlocked && p. target == null && msg.isAsynchronous();
                Message prev;
                for (;;) {
                    prev = p;
                    p = p. next;
                    if (p == null || when < p. when) {
                        break;
                    }
                    if (needWake && p.isAsynchronous()) {
                        needWake = false;
                    }
                }
                msg. next = p; // invariant: p == prev.next
                prev. next = msg;
            }
        }
        if (needWake) {
            //唤醒,无需再等待
            nativeWake( mPtr);
        }
        return true;
    }
}
 
 

你可能感兴趣的:(android)