Android消息机制其实指的就是Handler的消息机制。
ThreadLocal是如何获取数据的
public T get() { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t);//会先根据当前线程找到对应的ThreadLocalMap,如果没有就创建 if (map != null) { ThreadLocalMap.Entry e = map.getEntry(this); if (e != null) { @SuppressWarnings("unchecked") T result = (T)e.value; return result; } } return setInitialValue(); }//如果ThreadLocalMap 就会去创建ThreadLocalMap private T setInitialValue() { T value = initialValue(); Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value); return value; }
//上面的代码里面我们可以看到创建 ActivityThread的 Looper.prepareMainLooper(); Looper.loop(); 保证了 ActivityThread为主线程。
创建全局唯一Looper对象和全局唯一MessageQueue消息对象
消息处理
消息阻塞和延时
Looper 的阻塞主要是靠 MessageQueue 来实现的,在next()@MessageQuese 进行阻塞,在 enqueueMessage()@MessageQueue 进行唤醒。主要依赖 native 层的 Looper 依靠 epoll 机制进行的。
Message next() { int pendingIdleHandlerCount = -1; // -1 only during first iteration int nextPollTimeoutMillis = 0; for (;;) { if (nextPollTimeoutMillis != 0) { Binder.flushPendingCommands(); }//阻塞和延时,主要是next()中nativePollOnce(ptr, nextPollTimeoutMillis)调用naive方法操作管道 nativePollOnce(ptr, nextPollTimeoutMillis); } }
简单理解阻塞和唤醒 就是在主线程的MessageQueue没有消息时,便阻塞在loop的queue.next()中的nativePollOnce()方法里,此时主线程会释放CPU资源进入休眠状态,直到下个消息到达或者有事务发生,通过往pipe管道写端写入数据来唤醒主线程工作。 这里采用的epoll机制,是一种IO多路复用机制,可以同时监控多个描述符,当某个描述符就绪(读或写就绪),则立刻通知相应程序进行读或写操作,本质同步I/O,即读写是阻塞的。 所以说,主线程大多数时候都是处于休眠状态,并不会消耗大量CPU资源。 从阻塞到唤醒,消息切换
主要指enqueueMessage()消息入列是,上图代码对message对象池的重新排序,遵循规则(when从小到大)。 此处for死循环退出情况分两种 第一种:p==null表示对象池中已经运行到了最后一个,无需再循环。 第二种:碰到下一个消息when小于前一个,立马推出循环(不管对象池中所有message是否遍历完),进行重新排序。