曾以为对handler的源码了然于胸,直到在activity启动流程和rxjava的AndroidSchedulers.mainThread中的应用,让我感觉对它很是陌生,做个笔记。
以以下简单例子分析
Handler handler = new Handler() {
@Override
public void handleMessage(@NonNull Message msg) {
super.handleMessage(msg);
}
};
Message message = new Message();
// Message message1 = Message.obtain();
// Message.obtain(handler,callback);
handler.sendMessage(message);
1, 创建handler对象 handler 的创建,整个过程就是为了初始化mLooper,mQueue,mCallback,mAsynchronous
public Handler() {
this(null, false);
}
public Handler(@Nullable Callback callback) {
this(callback, false);
}
public Handler(@NonNull Looper looper) {
this(looper, null, false);
}
public Handler(@NonNull Looper looper, @Nullable Callback callback) {
this(looper, callback, false);
}
@UnsupportedAppUsage
public Handler(boolean async) {
this(null, async);
}
public Handler(@Nullable Callback callback, boolean async) {
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread " + Thread.currentThread()
+ " that has not called Looper.prepare()");
}
mQueue = mLooper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
@UnsupportedAppUsage
public Handler(@NonNull Looper looper, @Nullable Callback callback, boolean async) {
mLooper = looper;
mQueue = looper.mQueue;
mCallback = callback;
mAsynchronous = async;
}
2, 创建Message对象 有2 中方式,1 个是直接new,一个obtain。在obtain的情况下是复用mesage链表中已存在对象。避免重复创建对象。这里需要额外的注意一个变量Runnable callback。
public Message() {
}
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();
}
public static Message obtain(Message orig) {
Message m = obtain();
m.what = orig.what;
m.arg1 = orig.arg1;
m.arg2 = orig.arg2;
m.obj = orig.obj;
m.replyTo = orig.replyTo;
m.sendingUid = orig.sendingUid;
m.workSourceUid = orig.workSourceUid;
if (orig.data != null) {
m.data = new Bundle(orig.data);
}
m.target = orig.target;
m.callback = orig.callback;
return m;
}
public static Message obtain(Handler h) {
Message m = obtain();
m.target = h;
return m;
}
public static Message obtain(Handler h, Runnable callback) {
Message m = obtain();
m.target = h;
m.callback = callback;
return m;
}
public static Message obtain(Handler h, int what) {
Message m = obtain();
m.target = h;
m.what = what;
return m;
}
public static Message obtain(Handler h, int what, Object obj) {
Message m = obtain();
m.target = h;
m.what = what;
m.obj = obj;
return m;
}
public static Message obtain(Handler h, int what, int arg1, int arg2) {
Message m = obtain();
m.target = h;
m.what = what;
m.arg1 = arg1;
m.arg2 = arg2;
return m;
}
public static Message obtain(Handler h, int what,
int arg1, int arg2, Object obj) {
Message m = obtain();
m.target = h;
m.what = what;
m.arg1 = arg1;
m.arg2 = arg2;
m.obj = obj;
return m;
}
3, 调用handler的sendMessage方法发送message。
- 这里要注意一个值uptimeMillis,默认的值为当前的时间戳,如果发送延迟消息,则是在当前的时间戳的基础上做加法。
- message的target值为当前发送消息的handler。
public final boolean sendMessage(@NonNull Message msg) {
return sendMessageDelayed(msg, 0);
}
public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis) {
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
public boolean sendMessageAtTime(@NonNull 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);
}
private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg,
long uptimeMillis) {
msg.target = this;
msg.workSourceUid = ThreadLocalWorkSource.getUid();
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
4, 将当前的消息添加到MessageQueue,该对象是在Looper的构造方法中进行初始化。
调用MessageQueue的enqueueMessage方法将当前的消息插入到队列链表中。
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
boolean enqueueMessage(Message msg, long when) {
synchronized (this) {
msg.when = when;
Message p = mMessages;
boolean needWake;
// 如果当前队列为空,则让当前的消息指向p(null),mMessage(第一个元素)指向当前元素,
if (p == null || when == 0 || when < p.when) {
msg.next = p;
mMessages = msg;
} else {
// 如果队列不为空,让prev指向当前元素,p指向下一个元素,使用when来找到当前消息应该在的位置。当前的元素的next指向遍历出来的元素,遍历出来的前一个元素指向当前的元素。
Message prev;
for (;;) {
prev = p;
p = p.next;
if (p == null || when < p.when) {
break;
}
if (needWake && p.isAsynchronous()) {
needWake = false;
}
}
msg.next = p;
prev.next = msg;
}
}
return true;
}
5, Looper将message进行分发 主线程中的Looper的初始化在 ActivityThread的main方法中。 做了2件事 1,调用prepareMainLooper静态方法 2,调用loop静态方法
public static void main(String[] args) {
Looper.prepareMainLooper();
ActivityThread thread = new ActivityThread();
thread.attach(false, startSeq);
Looper.loop();
}
6, 给主线程创建了looper。其实调用的是myLooper方法。
public static void prepareMainLooper() {
prepare(false);
synchronized (Looper.class) {
if (sMainLooper != null) {
throw new IllegalStateException("The main Looper has already been prepared.");
}
sMainLooper = myLooper();
}
}
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
7, ThreadLocal类,这个类最大的特点,解决线程的同步性。就是在多个线程的情况下,每个线程只能获取自己线程内的对象(值)。
根据当前的线程获取到ThreadLocalMap,在getEntry方法中根据当前的threadLocation的hash值计算得出数组下标,来获取到元素。如果ThreadLocalMap为空创建对象,存入初始化的value并且返回。 到此主线程的Looper存入了主线程中。
public T get() {
// 获取当前的线程
Thread t = Thread.currentThread();
// 获取ThreadLocalMap
ThreadLocalMap map = getMap(t);
if (map != null) {
ThreadLocalMap.Entry e = map.getEntry(this);
if (e != null) {
T result = (T)e.value;
return result;
}
}
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;
}
8, loop进行循环Looper msg.target存放的是发送的handler对象,所以,即使有多个handler发送message,只有1个looper处理分发依然可以找到对应的handler对象。
public static void loop() {
final Looper me = myLooper();
final MessageQueue queue = me.mQueue;
for (;;) {
Message msg = queue.next();
try {
msg.target.dispatchMessage(msg);
} catch (Exception exception) {
} finally {
}
}
}
9, handler接收到message后,首先判断message中的callback是否为空,如果为空,则判断mCallback是否为空,否则调用handler的handleMessage方法,所以有2个callback可以在handleMessage方法之前进行message的拦截。
public void dispatchMessage(@NonNull Message msg) {
if (msg.callback != null) {
handleCallback(msg);
} else {
if (mCallback != null) {
if (mCallback.handleMessage(msg)) {
return;
}
}
handleMessage(msg);
}
}
调用callback对象的run方法。
private static void handleCallback(Message message) {
message.callback.run();
}