android源码系列-消息处理

开启消息处理

每个程序都有入口函数,android的入口函数在
ActivityThread#main,删减了部分不关注的代码

public static void main(String[] args) {
        
    // 初始化 Looper 对象
    Looper.prepareMainLooper();
    
    ActivityThread thread = new ActivityThread();
    thread.attach(false, startSeq);
    
    // 开启循环
    Looper.loop();
    
    // 不会调用,因为上面的代码阻塞了线程
    throw new RuntimeException("Main thread loop unexpectedly exited");
}

 public static void prepareMainLooper() {
    prepare(false);
    synchronized (Looper.class) {
        if (sMainLooper != null) {
            throw new IllegalStateException("The main Looper has already been prepared.");
        }
        // 
        sMainLooper = myLooper();
    }
}

// 静态变量,说明整个应用环境,只有这一个ThreadLocal<Looper>对象
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();

private static void prepare(boolean quitAllowed) {
    if (sThreadLocal.get() != null) {
        throw new RuntimeException("Only one Looper may be created per thread");
    }
    // 创建一个Looper对象,存储在sThreadLocal 对象中
    sThreadLocal.set(new Looper(quitAllowed));
}

private Looper(boolean quitAllowed) {
	// 床架looper的时候,同时创建了 MessageQueue
    mQueue = new MessageQueue(quitAllowed);
    mThread = Thread.currentThread();
}

// 获取当前线程的 Looper对象
public static @Nullable Looper myLooper() {
    return sThreadLocal.get();
}

消息循环处理流程

public static void loop() {
    // 开启死循环
    for (;;) {
        if (!loopOnce(me, ident, thresholdOverride)) {
            return;
        }
    }
}

private static boolean loopOnce(final Looper me,
            final long ident, final int thresholdOverride) {
     // 获取一个 Message 消息,下面有Message实体的数据结构
    Message msg = me.mQueue.next(); // might block
    if (msg == null) {
        // No message indicates that the message queue is quitting.
        return false;
    }

    // This must be in a local variable, in case a UI event sets the logger
    final Printer logging = me.mLogging;
    if (logging != null) {
        logging.println(">>>>> Dispatching to " + msg.target + " "
                + msg.callback + ": " + msg.what);
    }
    // 新加了一个回掉,在消息处理的前后调用的
    final Observer observer = sObserver;
    
    if (observer != null) {
        token = observer.messageDispatchStarting();
    }
    long origWorkSource = ThreadLocalWorkSource.setUid(msg.workSourceUid);
    try {
    	// 调用 msg 的 target对象的dispatchMessage
        msg.target.dispatchMessage(msg);
        if (observer != null) {
            observer.messageDispatched(token, msg);
        }
        dispatchEnd = needEndTime ? SystemClock.uptimeMillis() : 0;
    } catch (Exception exception) {
        if (observer != null) {
            observer.dispatchingThrewException(token, msg, exception);
        }
        throw exception;
    } finally {
        ThreadLocalWorkSource.restore(origWorkSource);
        if (traceTag != 0) {
            Trace.traceEnd(traceTag);
        }
    }
   
    msg.recycleUnchecked();

    return true;
}



/**
* 消息实体
*/
public final class Message implements Parcelable {
	public long when; // 队列按照时间排序
	
	Handler target;
    	Runnable callback;
	// 只有一个next引用,说明是一个单向链表
	Message next;
	
	// 缓存池
	private static Message sPool;
	private static int sPoolSize = 0;
	private static final int MAX_POOL_SIZE = 50;
}


// 上面  me.mQueue.next(); 调用了 MessageQueue 的next方法

Message next() {
    
    final long ptr = mPtr;
    if (ptr == 0) {
        return null;
    }

    int pendingIdleHandlerCount = -1; // -1 only during first iteration
    int nextPollTimeoutMillis = 0;
    for (;;) {
        if (nextPollTimeoutMillis != 0) {
            Binder.flushPendingCommands();
        }
        
		//判断是否需要等待 nextPollTimeoutMillis 的时间,如果 nextPollTimeoutMillis
		// 底层使用了epoll函数
        nativePollOnce(ptr, nextPollTimeoutMillis);

        synchronized (this) {
            
            final long now = SystemClock.uptimeMillis();
            Message prevMsg = null;
        	// 获取消息链表的头
            Message msg = mMessages;
            
        	// 如果消息队列不是空,并且消息的  target  是空,则进入循环处理
        	// msg.target == null 意味着是一个屏障消息,会找出该消息之后的第一个异步消息,然后返回
        	// 其实是优先处理了某一类消息
        	// 屏障消息只能系统调用,应用层无法调用
            if (msg != null && msg.target == null) {
                do {
                    prevMsg = msg;
                    msg = msg.next;
                } while (msg != null && !msg.isAsynchronous());
            }
            	// 普通消息的情况下
            if (msg != null) {
        		// 队头的消息还没到执行的事件点,计算当前时间与要执行时间的差值,用于后面等待唤醒的时间参数
                if (now < msg.when) {
                    nextPollTimeoutMillis = (int) Math.min(msg.when - now, Integer.MAX_VALUE);
                } else {
                    // msg 可以使用
                    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 (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;
    }
}

// 向消息队列中添加消息
boolean enqueueMessage(Message msg, long when) {
	
        msg.markInUse();
        msg.when = when;
        Message p = mMessages;
        boolean needWake;
        if (p == null || when == 0 || when < p.when) {
            // 插入队列的队首
            msg.next = p;
            mMessages = msg;
            needWake = mBlocked;
        } else {
           
            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;
}

public void dispatchMessage(@NonNull Message msg) {
	// 调用 handle.post 时会调用这个,callback 只会在handle所在线程发生调用
    if (msg.callback != null) {
    	// callback  处理msg 的callback
        handleCallback(msg);
    } else {
    	//  mCallback 是初始化 handle时传入的
    	// 如果有 mCallback ,则触发回掉,
        if (mCallback != null) {
            if (mCallback.handleMessage(msg)) {
                return;
            }
        }
        handleMessage(msg);
    }
}

总结

  1. 消息处理是在 ActivityThread 的main函数中启动的
  2. 启动后 Looper进入循环,获取 MessageQueue中的message进行处理
  3. 没有消息需要处理时调用 native 层的epoll wait 进入等待
  4. 当有消息插入 MessageQueue时,唤醒Looper中的 wait 等待,进入消息处理
  5. 调用 message 的 handleMessage 进行处理,如果有 callback 处理callback
  6. 消息入队用了 synchronized 保证插入数据线程安全
  7. 每个线程只有一个 Looper。因为每个线程都有一个 ThreadLocalMap ,存储的Looper对象也是线程唯一的
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值