当前位置:首页 > 知识库 > 正文

android源码怎么运行(android项目实例源码)

Android消息机制其实指的就是Handler的消息机制。

android源码怎么运行(android项目实例源码)  第1张

以上模型的解释:

1.以Handler的sendMessage方法为例,当发送一个消息后,会将此消息加入消息队列MessageQueue中。

2.Looper负责去遍历消息队列并且将队列中的消息分发给对应的Handler进行处理。

3.在Handler的handleMessage方法中处理该消息,这就完成了一个消息的发送和处理过程。 这里从图中可以看到参与消息处理有四个对象,它们分别是 Handler, Message, MessageQueue,Looper。

ThreadLocal 是一个线程内部的数据存储类,通过它可以在指定的线程中存储数据,数据存储以后,只有再指定线程中可以获取到存储的数据,对于其他线程来说则无法获取到数据。

我们看下ThreadLocal是如何存储数据的:

public void set(T value) { Thread t = Thread.currentThread(); ThreadLocalMap map = getMap(t); if (map != null) map.set(this, value); else createMap(t, value);}在源码里面我们可以看出 ThreadLocal在存储数据的时候,会先拿到当前线程,然后根据当前线程会拿到一个叫做ThreadLocalMap 的Map数组;

那么ThreadLocalMap 又是什么呢?

我们可以看到在CreateMap 里面是创建了ThreadLocalMap ,并且把我们当前线程当作Key,传递过去的 Value就是我们在调用ThreadLocal.set(T)传过来的值

void createMap(Thread t, T firstValue) { t.threadLocals = new ThreadLocalMap(this, firstValue);}android源码怎么运行(android项目实例源码)  第2张

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; }android源码怎么运行(android项目实例源码)  第3张

通过以上代码我们可以看出ThreadLocal是如何保证数据存储以后,只有再指定线程中可以获取到存储的数据,对于其他线程来说则无法获取到数据的了。

我们如何保证Acticity的默认线程是主线程的呢

在Acticity 中我们用到的线程是ActivityThread这个线程,在这个线程的

main(String[] args)方法里面我们可以看到下面代码 public static void main(String[] args) { Looper.prepareMainLooper(); ActivityThread thread = new ActivityThread(); thread.attach(false, startSeq); if (sMainThreadHandler == null) { sMainThreadHandler = thread.getHandler(); } if (false) { Looper.myLooper().setMessageLogging(new LogPrinter(Log.DEBUG, "ActivityThread")); } Trace.traceEnd(Trace.TRACE_TAG_ACTIVITY_MANAGER); Looper.loop(); throw new RuntimeException("Main thread loop unexpectedly exited"); }android源码怎么运行(android项目实例源码)  第4张

//上面的代码里面我们可以看到创建 ActivityThread的 Looper.prepareMainLooper(); Looper.loop(); 保证了 ActivityThread为主线程。

创建全局唯一Looper对象和全局唯一MessageQueue消息对象

android源码怎么运行(android项目实例源码)  第5张

Activity中创建Handler

android源码怎么运行(android项目实例源码)  第6张

android源码怎么运行(android项目实例源码)  第7张

消息发送

android源码怎么运行(android项目实例源码)  第8张android源码怎么运行(android项目实例源码)  第9张

消息处理

android源码怎么运行(android项目实例源码)  第10张android源码怎么运行(android项目实例源码)  第11张

消息阻塞和延时

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); } }android源码怎么运行(android项目实例源码)  第12张

阻塞和延时,主要是next()中nativePollOnce(ptr, nextPollTimeoutMillis)调用naive方法操作管道,由nextPollTimeoutMillis决定是否需要阻塞nextPollTimeoutMillis为0的时候表示不阻塞,为-1的时候表示一直阻塞直到被唤醒,其他时间表示延时。

唤醒

主要是指enqueueMessage()@MessageQueue 进行唤醒。

boolean enqueueMessage(Message msg, long when) { //在这里唤醒阻塞的方法 if (needWake) { nativeWake(mPtr); } }android源码怎么运行(android项目实例源码)  第13张

简单理解阻塞和唤醒 就是在主线程的MessageQueue没有消息时,便阻塞在loop的queue.next()中的nativePollOnce()方法里,此时主线程会释放CPU资源进入休眠状态,直到下个消息到达或者有事务发生,通过往pipe管道写端写入数据来唤醒主线程工作。 这里采用的epoll机制,是一种IO多路复用机制,可以同时监控多个描述符,当某个描述符就绪(读或写就绪),则立刻通知相应程序进行读或写操作,本质同步I/O,即读写是阻塞的。 所以说,主线程大多数时候都是处于休眠状态,并不会消耗大量CPU资源。 从阻塞到唤醒,消息切换

android源码怎么运行(android项目实例源码)  第14张

延时入队

android源码怎么运行(android项目实例源码)  第15张

主要指enqueueMessage()消息入列是,上图代码对message对象池的重新排序,遵循规则(when从小到大)。 此处for死循环退出情况分两种 第一种:p==null表示对象池中已经运行到了最后一个,无需再循环。 第二种:碰到下一个消息when小于前一个,立马推出循环(不管对象池中所有message是否遍历完),进行重新排序。

发表评论

最新文章

推荐文章