【Android源码篇】 深入剖析Handler内部原理

Handler作为Android应用层开发,进程通信一大重点,可以说是使用最频繁的一个机制,不管是IntentService,ThreadHandler都绕不开它。本文详解Handler机制的内部源码

深入剖析Handler,没有看错,比别人更深更精准!

看本文可以回答你这几个问题:

  1. UI线程的Looper在哪里创建?

  2. MessageQueue真的是个队列吗?

  3. 延迟处理机制的原理?

  4. Handler中的Message同步和MessageQueue同步?

一、Handler介绍

Handler在Android os包下,当我们创建Handler时,它会绑定一个线程,并且创建一个消息队列,通过发送Message或者Runnable对象到队列并轮询取出,实现关联。
我们常用的Handler功能是,定时执行Runnable或者处理不同线程通信的问题,比如UI线程和子线程等。
由此可见Handler内部机制中的几大元素:Handler,Message,MessageQueue,Looper,ThreadLocal等,接下来,分别查看它的内部源码。

二、Handler源码剖析

Handler作为封装对外的处理器,我们来看看它对外的接口内部是做了哪些操作。

1. Handler构造函数:

它的构造函数,我归纳为三种方式,分别是:
1.传入自定义Looper对象,
2.继承Handler实现Callback接口模式,
3.默认创建的Looper模式,
其中2,3是我们常用的,当然1和2也能同时使用,callback接口中实现handleMessage,用于我们自定义Handler是实现回调用的。还有个被hide隐藏的传参,async是否同步,默认是不同步,且不支持设置同步模式。

可以传入自定义的Looper,Callback接口

public Handler(Looper looper, Callback callback, boolean async) {
	mLooper = looper;
	mQueue = looper.mQueue;
	mCallback = callback;
	mAsynchronous = async;
}

常规的构造方法如下:

  • 其中FIND_POTENTIAL_LEAKS标签是检查“继承类是否为非静态内部类”标签,我们知道,非静态内部类持有对象,容易导致内存泄漏的问题,可以查看我的《Android内存优化分析篇》

  • mAsynchronous可以看到一直是false

       public Handler(Callback callback, boolean async) {
          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());
              }
          }
    
    
          mLooper = Looper.myLooper();
          if (mLooper == null) {
              throw new RuntimeException(
                  "Can't create handler inside thread that has not called Looper.prepare()");
          }
          mQueue = mLooper.mQueue;
          mCallback = callback;
          mAsynchronous = async;
      }
    

2. 创建Looper对象和mQueue消息队列

由上构造函数中调用Looper.myLooper();创建了Looper对象,并取用了新创建Looper对象内部的mQueue队列,详解下Looper分析

3. sendMessage

  • 其中sendEmptyMessage通过obtion新获取了一个Message对象

      public final boolean sendEmptyMessageDelayed(int what, long delayMillis) {
          Message msg = Message.obtain();
          msg.what = what;
          return sendMessageDelayed(msg, delayMillis);
      }
    
  • 发送消息:sendMessageDelayed—>sendMessageAtTime—>enqueueMessage

  • 注意到,在调用sendMessageAtTime时,传入的时间值: 系统时钟+delayMillis

  • 其中将 msg.target标记为当前Handler对象

  • 最终调用了MessageQueue的enqueueMessage,看后面MessageQueue分析

      //----------1
      public final boolean sendMessage(Message msg)
      {
          return sendMessageDelayed(msg, 0);
      }
      //----------2
       public final boolean sendMessageDelayed(Message msg, long delayMillis)
      {
          if (delayMillis < 0) {
              delayMillis = 0;
          }
          return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
      }
      //---------3
      public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
          MessageQueue queue = mQueue;
          if (queue == null) {
              RuntimeException e = new RuntimeException(
                      this + " sendMessageAtTime() called with no mQueue");
              Log.w("Looper", e.getMessage(), e);
              return false;
          }
          return enqueueMessage(queue, msg, uptimeMillis);
      }
      //----------4
      private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
          msg.target = this;
          if (mAsynchronous) {
              msg.setAsynchronous(true);
          }
          return queue.enqueueMessage(msg, uptimeMillis);
      }
    

4. removeMessages

从队列删除

5. post(Runnable r)

  • 在getPostMessage中讲Runnable封装成了Message对象

      public final boolean post(Runnable r)
      {
         return  sendMessageDelayed(getPostMessage(r), 0);
      }
    
       private static Message getPostMessage(Runnable r) {
          Message m = Message.obtain();
          m.callback = r;
          return m;
      }
    

6. dispatchMessage和handlerMessage

  • 我们看到dispatchMessage调用了callback和handlerMessage分发Message结果

  • 那么,前面我们看到了经常调用的sendMessage,那么回调是在什么时候调用的呢?

  • 让我们接下来一起看看Looper类吧。

      public void dispatchMessage(Message msg) {
          if (msg.callback != null) {
              handleCallback(msg);
          } else {
              if (mCallback != null) {
                  if (mCallback.handleMessage(msg)) {
                      return;
                  }
              }
              handleMessage(msg);
          }
      }
    
       public void handleMessage(Message msg) {}
    

三、Looper源码剖析

看looper做了什么,首先看mylooper方法,还记得吗,在Handler初始化时创建Looper对象调用的方法

1. myLooper方法

  • 调用sThreadLocal取出一个looper对象

      public static @Nullable Looper myLooper() {
              return sThreadLocal.get();
        }
    
      // sThreadLocal.get() will return null unless you've called prepare().
      static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
    

2. Looper.prepare()创建对象

  • 上面看到mylooper从sThreadLocal取出,但是什么时候存的呢,looper又是如何创建?

  • 由下看出Looper通过prepare创建并存入sThreadLocal,在构造同时创建MessageQueue

  • 标记成员mThread为当前线程

  • quitAllowed标识能否安全退出

      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();
      }
    

3. UI线程调用Handler,Looper怎么创建

  • prepareMainLooper:在当前线程初始化looper,在ActivityThread调用,也就是我们创建Activity时已经创建了Looper了

  • prepare(false):由于在ActivityThread创建,是不能安全退出的

      /**
       * Initialize the current thread as a looper, marking it as an
       * application's main looper. The main looper for your application
       * is created by the Android environment, so you should never need
       * to call this function yourself.  See also: {@link #prepare()}
       */
      public static void prepareMainLooper() {
          prepare(false);
          synchronized (Looper.class) {
              if (sMainLooper != null) {
                  throw new IllegalStateException("The main Looper has already been prepared.");
              }
              sMainLooper = myLooper();
          }
      }
    
       //-------->ActivityThread: Main:
       public static void main(String[] args) {
    
          ---
    
          Looper.prepareMainLooper();
    
          ActivityThread thread = new ActivityThread();
          thread.attach(false);
    
          if (sMainThreadHandler == null) {
              sMainThreadHandler = thread.getHandler();
          }
    
          if (false) {
              Looper.myLooper().setMessageLogging(new
                      LogPrinter(Log.DEBUG, "ActivityThread"));
          }
    
          // End of event ActivityThreadMain.
          Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER);
          Looper.loop();
      }
    

4. Looper.loop()

  • UI线程创建Looper,上ActivityThread中,在调用prepare后接着调用Looper.loop
  • loop通过 for (;;)死循环,从queue中取下一则消息
  • 其中 msg.target.dispatchMessage(msg);,在上面Handler中将handler对象传给了looper
	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;

		//--------------确保同一进程
        Binder.clearCallingIdentity();
        final long ident = Binder.clearCallingIdentity();

        for (;;) {
            Message msg = queue.next(); // might block
            if (msg == null) {
                // No message indicates that the message queue is quitting.
                return;
            }

            //--------------打印日志
            final Printer logging = me.mLogging;
            if (logging != null) {
                logging.println(">>>>> Dispatching to " + msg.target + " " +
                        msg.callback + ": " + msg.what);
            }
			
			//--------------从队列中获取分发消息延时
            final long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
			
			//--------------Trace标记,用于记录message分发完成
            final long traceTag = me.mTraceTag;
            if (traceTag != 0 && Trace.isTagEnabled(traceTag)) {
                Trace.traceBegin(traceTag, msg.target.getTraceName(msg));
            }
            final long start = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
            final long end;
            try {
                msg.target.dispatchMessage(msg);
                end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
            } finally {
                if (traceTag != 0) {
                    Trace.traceEnd(traceTag);
                }
            }
            if (slowDispatchThresholdMs > 0) {
                final long time = end - start;
                if (time > slowDispatchThresholdMs) {
                    Slog.w(TAG, "Dispatch took " + time + "ms on "
                            + Thread.currentThread().getName() + ", h=" +
                            msg.target + " cb=" + msg.callback + " msg=" + msg.what);
                }
            }

            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);
            }
			
			//--------------充值message对象参数
            msg.recycleUnchecked();
        }
    }

四、MessageQueue源码剖析

MessageQueue主要分析插入和取出,由下enqueueMessage插入方法看出,它名字带着Queue,但其实并不是,它实际是个单链表结构,通过native操作指针,去进行msg的读取操作。当然,这更加快捷的实施取出,删除和插入操作。

1. enqueueMessage

  • msg.markInUse();标记当前msg正在使用

  • 其中mMessages是可以理解为即将执行的Message对象

  • 将当前mMessages新传入的Msg设置触发时间对比,如果新的Msg设置时间早,则将2者位置对调,将新的排前面,与之对比的mMessages排到其后。反之,则与mMessages后一个对比时间,依次类比,插入到队列中

  • 其中,如果msg事Asynchronous同步的,那么它只能等到上一个同步msg执行完,才能被唤醒执行。

      boolean enqueueMessage(Message msg, long when) {
     	...
    
         synchronized (this) {
             if (mQuitting) {
                 //------------->抛出一个IllegalStateException
                 Log.w(TAG, e.getMessage(), e);
                 msg.recycle();
                 return false;
             }
     		//------------->标记当前msg正在使用
             msg.markInUse();
             msg.when = when;
             Message p = mMessages;
             boolean needWake;
             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.
                 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;
             }
    
             // We can assume mPtr != 0 because mQuitting is false.
             if (needWake) {
                 nativeWake(mPtr);
             }
         }
         return true;
     }
    

2. next取出

  • 可以看出enqueueMessage和next都是同步的

  • 通过循环,把mMessages当前msg

  • 比较当前时间和Msg标记时间,如果早的话就设置一段指针查找超时时间

  • 将msg标记为使用,并取出消息返回

      Message next() {
    
          //----->当消息轮询退出时,mPtr指针找不到地址,返回空取不到对象
          final long ptr = mPtr;
          if (ptr == 0) {
              return null;
          }
      	//----->同步指针查找的时间,根据超时时间计算,比如当前未到msg的时间,指针会在一段计算好的超时时间后去查询
          int pendingIdleHandlerCount = -1; // -1 only during first iteration
          int nextPollTimeoutMillis = 0;
          for (;;) {
              if (nextPollTimeoutMillis != 0) {
                  Binder.flushPendingCommands();
              }
    
              nativePollOnce(ptr, nextPollTimeoutMillis);
    
              synchronized (this) {
                  // Try to retrieve the next message.  Return if found.
                  final long now = SystemClock.uptimeMillis();
                  Message prevMsg = null;
                  Message msg = mMessages;
      				
      			//----->如果target为null,寻找下一个带“同步”标签的msg
    
                  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) {
    
      				//----->比较当前时间和Msg标记时间,如果早的话就设置一段指针查找超时时间
    
                      if (now < msg.when) {
                          // Next message is not ready.  Set a timeout to wake up when it is ready.
                          nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                      } else {
                          // Got a message.
                          mBlocked = false;
                          if (prevMsg != null) {
                              prevMsg.next = msg.next;
                          } else {
                              mMessages = msg.next;
                          }
                          msg.next = null;
                          if (DEBUG) Log.v(TAG, "Returning message: " + msg);
                          msg.markInUse();
                          return msg;
                      }
                  } else {
                      // No more messages.
                      nextPollTimeoutMillis = -1;
                  }
    
                  // Process the quit message now that all pending messages have been handled.
                  if (mQuitting) {
                      dispose();
                      return null;
                  }
    
                  // 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
    
                  boolean keep = false;
                  try {
                      keep = idler.queueIdle();
                  } catch (Throwable t) {
                      Log.wtf(TAG, "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.
              nextPollTimeoutMillis = 0;
          }
      }
    

3. quit操作

  • 前面标记是否能安全退出,否则报错

  • 退出后唤醒指针,接触msg的锁

       void quit(boolean safe) {
              if (!mQuitAllowed) {
                  throw new IllegalStateException("Main thread not allowed to quit.");
              }
      
              synchronized (this) {
                  if (mQuitting) {
                      return;
                  }
                  mQuitting = true;
      
                  if (safe) {
                      removeAllFutureMessagesLocked();
                  } else {
                      removeAllMessagesLocked();
                  }
      
                  // We can assume mPtr != 0 because mQuitting was previously false.
                  nativeWake(mPtr);
              }
          }
    

五、Message源码剖析

Message主要是一个Parcelable序列号对象,封装了不分信息和操作,它构造了一个对象池,这也是为什么我们一直发送msg,不会内存爆炸的原因,来看看实现

1. obtain()

  • 维持一个大小为50的同步线程池

  • 这里可以看出Message是个链表结构,obtain将sPool取出return Message,并对象池下一个msg标记为sPool

      private static final int MAX_POOL_SIZE = 50;
      
      ...
    
      public static Message obtain() {
          synchronized (sPoolSync) {
              if (sPool != null) {
                  Message m = sPool;
                  sPool = m.next;
                  m.next = null;
                  m.flags = 0; // clear in-use flag
                  sPoolSize--;
                  return m;
              }
          }
          return new Message();
      }
    

2.recycleUnchecked 回收消息

  • 回收初始化当前msg

  • 如果当前对象池大小小于MAX_POOL_SIZE,则将初始化后的msg放到表头sPool,sPoolSize++。

  • 由此可以看出,如果每次new新的Message传入Handler,必然增加内存消耗,通过obtain服用才是正确的做法

      /**
       * Recycles a Message that may be in-use.
       * Used internally by the MessageQueue and Looper when disposing of queued Messages.
       */
      void recycleUnchecked() {
          // Mark the message as in use while it remains in the recycled object pool.
          // Clear out all other details.
          flags = FLAG_IN_USE;
          what = 0;
          arg1 = 0;
          arg2 = 0;
          obj = null;
          replyTo = null;
          sendingUid = -1;
          when = 0;
          target = null;
          callback = null;
          data = null;
    
          synchronized (sPoolSync) {
              if (sPoolSize < MAX_POOL_SIZE) {
                  next = sPool;
                  sPool = this;
                  sPoolSize++;
              }
          }
      }
    

3. Message标签:是否使用,同步标签

public void setAsynchronous(boolean async) {
    if (async) {
        flags |= FLAG_ASYNCHRONOUS;
    } else {
        flags &= ~FLAG_ASYNCHRONOUS;
    }
}

/*package*/ boolean isInUse() {
    return ((flags & FLAG_IN_USE) == FLAG_IN_USE);
}

/*package*/ void markInUse() {
    flags |= FLAG_IN_USE;
} 

六、总结

废了小半天功夫,整理了对Handler源码的阅读总结,虽然东西很多也很繁琐,不过如果认真去看,是不是发现越深入就越有趣,也越发发现Android源码的严谨性。平常至少简单一用,只要深入了解才能更好地去使用它理解它,比如Message对象池的应用,这不就是享元模式吗,希望大家都能有所体悟。

猜你喜欢

转载自blog.csdn.net/sslinp/article/details/84451313
今日推荐