在消息机制里面,有一个非常重要的东西,那就是Looper,Looper的作用主要是从消息队列里面取出消息交给Handler处理,不过不仅限于此,在这里面还有很多东西值得我们去源码看一看:
1.从Looper.prepare()开始
要在一个线程里面处理消息,代码如下:
class LooperThread extends Thread
{
public Handler mHandler;
public void run()
{
Looper.prepare();
mHandler = new Handler()
{
public void handleMessage(Message msg)
{
// process incoming messages here
}
};
Looper.loop();
}
首先就必须要先调用Looper.prepare(),那这个方法做了些什么呢:
public static void prepare() {
prepare(true);
}
private static void prepare(boolean quitAllowed) {
if (sThreadLocal.get() != null) {
throw new RuntimeException(“Only one Looper may be created per thread”);
}
sThreadLocal.set(new Looper(quitAllowed));
}
代码其实只有关键性的一句,就是sThreadLocal.set(new Looper(quitAllowed)),首先来看看sThreadLocal
static final ThreadLocal sThreadLocal = new ThreadLocal();
ThreadLocal:代表了一个线程局部的变量,每条线程都只能看到自己的值,并不会意识到其它的线程中也存在该变量。
在这里ThreadLocal的作用是保证了每个线程都有各自的Looper
上面的判断也说明了一个问题:一个线程只能有一个Looper
接下来看看创建Looper实例的方法new Looper(quitAllowed):
final MessageQueue mQueue;
final Thread mThread;
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
在构造方法里,初始化了MessageQueue和代表当前线程的属性mThread,关于MessageQueue可以看看文章开头的链接,里面有详细的代码解析,这里就不赘述了。
调用Looper.prepare()其实就是利用ThreadLocal为当前的线程创建了一个独立的Looper,这其中包含了一个消息队列
2.创建Handler->new Handler()
在为当前线程创建了Looper之后,就可以创建Handler来处理消息了,这里可以解决我们一个疑问:
Handler是怎么跟Looper关联上的?
//全局变量
final Looper mLooper;
final MessageQueue mQueue;
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 = callback;
mAsynchronous = async;
}
在Handler中有两个全局变量mLooper和mQueue代表当前Handler关联的Looper和消息队列,并在构造函数中进行了初始化,重要的就是调用了:Looper.myLooper():
public static @Nullable Looper myLooper() {
return sThreadLocal.get();
}
Android高级架构师
由于篇幅问题,我呢也将自己当前所在技术领域的各项知识点、工具、框架等汇总成一份技术路线图,还有一些架构进阶视频、全套学习PDF文件、面试文档、源码笔记。
- 330页PDF Android学习核心笔记(内含上面8大板块)
-
Android学习的系统对应视频
-
Android进阶的系统对应学习资料
- Android BAT部分大厂面试题(有解析)
好了,以上便是今天的分享,希望为各位朋友后续的学习提供方便。觉得内容不错,也欢迎多多分享给身边的朋友哈。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
-1715834356188)]
- Android BAT部分大厂面试题(有解析)
[外链图片转存中…(img-TmNYbs9m-1715834356190)]
好了,以上便是今天的分享,希望为各位朋友后续的学习提供方便。觉得内容不错,也欢迎多多分享给身边的朋友哈。
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!