Android的消息机制中有三个重要的类:Handler、MessageQueue和Looper。其中MessageQueue是先进先出的消息队列,它存储一组消息,有插入和删除的功能;Looper是循环的意思,主要功能是轮询MessageQueue里面的消息,然后交由Handler处理,如果暂时没有消息,则会等待;Handler主要统筹Looper和MessageQueue的功能,实现消息的发送和处理。
在Handler中,一个线程最多只有一个Looper,但线程默认是没有Looper的,需要自己创建,在Android的UI线程中可以直接使用Handler是因为在创建主线程的时候已经初始化了Looper;在Handler机制中有一个叫ThreadLocal的类,ThreadLocal不是一个线程,它的作用是可以为每个不同的线程存储数据,并且这些不同线程的数据互不干扰,而Looper就是通过它来保存在线程中的,通过调用ThreadLocal的get方法,可以获取当前线程的Looper对象。
ThreadLocl的工作原理
先来看一个例子:
private java.lang.ThreadLocal<String> threadLocal = new ThreadLocal<>();
public void test() {
threadLocal.set("主线程");
LogUtils.d(Thread.currentThread().getName() + "的值=" + threadLocal.get());
new Thread("线程01") {
@Override
public void run() {
super.run();
threadLocal.set("线程01");
Log.d("tag", Thread.currentThread().getName() + "的值=" + threadLocal.get());
}
}.start();
new Thread("线程02") {
@Override
public void run() {
super.run();
//threadLocal.set("线程02");
Log.d("tag", Thread.currentThread().getName() + "的值=" + threadLocal.get());
}
}.start();
}
执行test方法结果如下:
从执行结果来看,threadlocal确实可以为不同的线程保存各自的数据,来看看源码是怎么实现的:
public void set(T value) {
//获取当前线程
Thread t = Thread.currentThread();
//获取当前线程的threadlocalmap
ThreadLocalMap map = getMap(t);
//如果threadlocalmap不为null,那么将value值保存,其中key值为当前threadlocal
if (map != null)
map.set(this, value);
else
createMap(t, value);
}
//返回线程的threadlocalmap对象
ThreadLocalMap getMap(Thread t) {
return t.threadLocals;
}
//创建对应线程的threadlocalmap实例,并且保存对应的值
void createMap(Thread t, T firstValue) {
t.threadLocals = new ThreadLocalMap(this, firstValue);
}
其实这里的关键是将value值保存到当前线程的threadlocalmap中去,而且对应的key是threadlocal本身,来看看threadlocal的get方法:
public T get() {
//获取对应的线程
Thread t = Thread.currentThread();
//获取对应线程的threadlocalmap
ThreadLocalMap map = getMap(t);
//如果不为null,将取出key为当前threadlocal对应的value
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
@SuppressWarnings("unchecked")
T result = (T)e.value;
return result;
}
}
//threadlocalmap为null的时候,返回setInitialValue的值,其实就是null
return setInitialValue();
}
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;
}
protected T initialValue() {
return null;
}
代码都不复杂,当我们调用threadlocal的set方法保存某个value的时候,其实在set方法内部会获取当前线程的threadlocalmap,然后将value保存到threadlocalmap中,当我们调用threadlocal的get方法的时候,同样也是首先获取到当前线程的threadlocalmap,将保存在其中的value取出来
大概就是每个Thread都有自己的ThreadLocalMap,在ThreadLocal保存数据的时候,会根据当前线程对象拿到对应的ThreadLocalMap并将数据保存到里面,然后取数据的时候,也是先拿到对应Thread的ThreadLocalMap对象, 再取其中的数据,说白了线程能独立保存数据而不受其他线程的干扰,就是因为保存数据的时候首先获取当前线程对象,然后将数据保存到对应线程的ThreadLocalMap中
MessageQueue的工作原理
messagequeue的插入和读取操作内部是由先进先出的单链表结构来实现的,可能是因为单链表结构在插入和删除上效率比较高吧,插入用enqueueMessage方法,读取用next方法来实现,下面我们来看看他们的源码:
boolean enqueueMessage(Message msg, long when) {
if (msg.target == null) {
throw new IllegalArgumentException("Message must have a target.");
}
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}
synchronized (this) {
if (mQuitting) {
IllegalStateException e = new IllegalStateException(
msg.target + " sending message to a Handler on a dead thread");
Log.w(TAG, e.getMessage(), e);
msg.recycle();
return false;
}
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;
}
上面是消息的插入操作,注意参数when代表这个消息何时取出来处理。下面我们看看,next方法源码:
Message next() {
// Return here if the message loop has already quit and been disposed.
// This can happen if the application tries to restart a looper after quit
// which is not supported.
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();
}
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;
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.
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;
}
}
可以看到,next是一个无限循环的方法,当取到消息的时候返回msg,没有消息的时候,会一直阻塞直到有消息。注意上面的一个判断,如果now<msg.when,就先不取msg,其实就是说这个消息还没到执行时间,所以暂时不会取出来,这个就是Handler消息可以延时执行的原因了。当looper执行退出的时候,即上面的mQuitting为true的时候,此时next方法返回null,然后loop方法就会跳出循环。
Looper的工作原理
looper在消息机制中扮演者消息循环的角色,就是说它会不停地从消息队列中查看有没有新消息,如果取出了新消息就会马上处理,否则就阻塞在那里。我们知道,开启looper的工作,需要首先在对应的线程中通过Looper.prepare()创建一个looper,然后通过loop方法来开启消息的循环,
我们来看看这个两个方法的源码:
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
//一个线程只能创建一个looper
if (sThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
//创建一个looper实例,通过threadlocal保存起来
sThreadLocal.set(new Looper(quitAllowed));
}
来看看looper的构造方法:
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
可以看到,构造方法中会创建一个消息队列和获取当前looper的线程。looper还提供了prepareMainLooper方法,这个方法是用来给主线程ActivityThread创建looper时使用的,当然其本质也是通过prepare方法来实现的。由于主线程的looper比较特殊,所以其提供了getMainLooper方法,用来在任何地方都可以获取主线程的looper对象。
Looper提供了两个退出方法:quit和quitSafely,其中quit是立即退出,quitSafely是等消息队列中的消息处理完毕以后再安全退出。如果我们手动创建了looper,在不需要的是要手动去退出,否则这个子线程会一直处于等待状态。下面我们来看看looper最关键的方法loop,只有调用了loop方法,消息循环系统才会真正起作用,如下:
public static void loop() {
//myLooper会从ThreadLocal中返回Looper对象,这个Looper对象就是当前线程创建的Looper对象
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 (;;) {
//调用消息队列的next方法取消息
Message msg = queue.next(); // might block
//如果消息为null,结束方法
if (msg == null) {
// No message indicates that the message queue is quitting.
return;
}
// 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 long slowDispatchThresholdMs = me.mSlowDispatchThresholdMs;
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就是处理这个消息的handler,将消息递交给handler处理
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);
}
msg.recycleUnchecked();
}
}
loop方法还是比较好理解的,首先这是一个循环,唯一跳出循环的条件是取到的消息为null,就是当我们调用了looper的退出方法的时候,同时也会去调用messagequeue的退出方法,此时next方法才会返回一个null值。当我们取出了新消息,后面就调用msg.target.dispatchMessage(msg),这里的target是发送消息的handler对象,这样消息又交给了handler来处理了。
值得注意的是,我们取出消息调用handler的dispatchMessage方法的过程是在looper的loop方法中执行的,而loop方法是创建handler对象的线程中被调用的,所以不管handler在哪个线程发送消息,最终处理消息都是在loop方法中执行,这也就实现了线程的切换。
Handler的工作原理
handler的工作主要包括消息的发送和接收,消息的发送主要用post和send系列方法,接收一般是通过重写对应的handleMessage方法。下面我们先来看看一个例子:
public class HandlerDemo {
//重写handler的handleMessage方法
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
LogUtils.d("handler接收到消息,what内容=" + msg.what+",当前线程为:"+Thread.currentThread().getName());
}
};
public void sendMsg() {
//在一个子线程中通过主线程的handler发送消息给主线程处理
new Thread(new Runnable() {
@Override
public void run() {
try {
//延时两秒
TimeUnit.SECONDS.sleep(2);
//发送一个消息给handler处理
Message msg = new Message();
msg.what = 01;
handler.sendMessage(msg);
} catch (InterruptedException e) {
}
}
}).start();
}
}
执行结果:
可以看到,例子中创建了主线程的handler对象,演示了在子线程中通过sendMessage方法发送消息,然后在handleMessage方法中处理消息,并且处理消息的线程的为主线程,这样就实现了从子线程切换到主线程;
下面来看看对应的源码:
public final boolean sendMessage(Message msg)
{
return sendMessageDelayed(msg, 0);
}
public final boolean sendMessageDelayed(Message msg, long delayMillis)
{
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
public boolean sendMessageAtTime(Message msg, long uptimeMillis) {
//mQueue为当前handler的消息队列
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);
}
private boolean enqueueMessage(MessageQueue queue, Message msg, long uptimeMillis) {
msg.target = this;
if (mAsynchronous) {
msg.setAsynchronous(true);
}
//将消息保存到消息队列中
return queue.enqueueMessage(msg, uptimeMillis);
}
上面的关键作用是把消息保存到handler对象的messagequeue中去,值得注意的地方,msg.target=this,这里给消息的target赋值为当前的handler对象;
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());
}
}
//获取对应的looper实例
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;
}
在handler的构造方法中,会初始化Looper对象和消息队列mQueue,在子线程中创建handler对象的时候,也是需要调用Looper对象的loop方法的,执行了loop方法以后,就开始轮询消息队列的消息了,取到消息以后就会调用handler的dispatchMessage方法
dispatchMessage方法源码:
public void dispatchMessage(Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
if的判断,如果msg.callback!=null,则调用handleCallback,其实msg.callback是post方法中的runnable对象,我们来看看:
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;
}
再来看看handleCallback(msg),就是调用runnable的run方法,这就是handler的post方法的原理。
private static void handleCallback(Message message) {
message.callback.run();
}
如果mCallback不为努力,则执行它的handleMessage方法,那mCallback 又是什么?
public Handler(Callback callback) {
this(callback, 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赋值
mCallback = callback;
mAsynchronous = async;
}
//Callback接口
public interface Callback {
/**
* @param msg A {@link android.os.Message Message} object
* @return True if no further handling is desired
*/
public boolean handleMessage(Message msg);
}
可以看到,其实这个mCallback就是我们在例子中创建handler的时候传递的回调参数。
如果这个回调方法也为null,我们就调用handleMessage(msg)方法处理,这就是通常我们在创建handler的时候重写的那个方法了
好了,关于android消息机制的工作原理,通过上面的讲解我们大概也清楚是怎么一回事了。