1.使用过Handler吗?Handler是用来干嘛的?
答:使用过,Handler是android中设计的用于线程间通信的工具类,针对单个Handler对象而言,与其关联的有Message,MessageQueue和Looper,其中Message存储于MessageQueue中,Looper从MessageQueue中取消息并处理,Handler将Message添加到MessageQueue中。
Message
Message 即消息,是信息的载体。它包含了各种用于传递数据的字段,如what
可用于标识消息类型,arg1
、arg2
能携带简单的整型数据,obj
可用于传递任意对象。当我们需要在不同线程间传递数据或指令时,就会创建 Message 对象。并且,Message 可以通过Message.obtain()
方法从消息池中获取已存在的 Message 对象进行重用,避免频繁创建新对象带来的内存开销。这是因为在Message
类内部维护了一个静态的单链表Message sPool
作为消息池,当调用obtain()
方法时,会先检查消息池是否有可用的 Message 对象,如果有则直接返回,若没有则创建一个新的。其相关源码如下:
public final class Message implements Parcelable {
// 消息池的头节点
private static Message sPool;
// 消息池中消息的数量
private static int sPoolSize = 0;
// 其他众多属性...
public static Message obtain() {
synchronized (sPoolSync) {
if (sPool != null) {
Message m = sPool;
sPool = m.next;
m.next = null;
sPoolSize--;
return m;
}
}
return new Message();
}
}
MessageQueue
MessageQueue 被称为消息队列,虽名为队列,但实际上是一个以msg.what
自小向大排序的头插单链表。它负责存储 Message 对象。当 Handler 发送消息时,消息会被添加到 MessageQueue 中。在MessageQueue
的enqueueMessage
方法中,会对新加入的消息根据其when
(消息执行时间)进行排序插入,以确保消息按顺序执行。该方法源码部分如下:
boolean enqueueMessage(Message msg, long when) {
// 检查target是否为空,target即发送此消息的Handler
if (msg.target == null) {
throw new IllegalArgumentException("Message must have a target.");
}
// 加锁保证多线程安全
synchronized (this) {
// 检查消息是否已在使用中
if (msg.isInUse()) {
throw new IllegalStateException(msg + " This message is already in use.");
}
// 检查消息队列是否正在关闭
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) {
// 将新消息插入到队列头部
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;
prev.next = msg;
}
// 如果需要唤醒等待的线程(例如在Looper阻塞等待消息时)
if (needWake) {
nativeWake(mPtr);
}
}
return true;
}
Looper
Looper 被称为循环器,负责不断地从 MessageQueue 中取出消息,并将其分发给对应的 Handler 进行处理。一个线程只能有一个 Looper 实例,它在创建时会绑定到特定的线程上。在Looper
类中,通过ThreadLocal
来确保每个线程拥有独立的 Looper 实例。prepare
方法用于创建 Looper 对象,并为其关联一个 MessageQueue,同时将 Looper 对象存储到ThreadLocal
中,代码如下:
public class Looper {
// 用于存储每个线程的Looper实例
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
// Looper关联的消息队列
final MessageQueue mQueue;
// 当前线程
final Thread mThread;
// 其他属性...
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
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));
}
}
而loop
方法则是启动消息循环,它会进入一个无限循环,不断地从 MessageQueue 中取出消息并处理,直到消息队列为空或 Looper 被停止。
public static void loop() {
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;
// 确保主线程的Looper不会被GC回收
Binder.clearCallingIdentity();
final long ident = Binder.clearCallingIdentity();
for (;;) {
Message msg = queue.next();
if (msg == null) {
return;
}
// 处理消息
msg.target.dispatchMessage(msg);
msg.recycleUnchecked();
}
}
Handler
与 ThreadLocal
的结合使用
1. Looper
中的 ThreadLocal
应用
在 Handler
机制中,Looper
起着关键作用。每个线程只能有一个 Looper
,Looper
的创建和管理就使用了 ThreadLocal
。Looper
类中有一个静态的 ThreadLocal
变量 sThreadLocal
,用于存储每个线程的 Looper
实例。以下是 Looper
类中的部分代码:
public final class Looper {
// 静态的 ThreadLocal 变量,用于存储每个线程的 Looper 实例
static final ThreadLocal<Looper> sThreadLocal = new ThreadLocal<Looper>();
private static Looper sMainLooper; // guarded by Looper.class
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
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));
}
public static Looper myLooper() {
// 获取当前线程的 Looper 实例
return sThreadLocal.get();
}
}
在上述代码中,sThreadLocal
确保了每个线程都有自己独立的 Looper
实例。prepare()
方法用于为当前线程创建 Looper
实例,并将其存储到 sThreadLocal
中。myLooper()
方法用于获取当前线程的 Looper
实例。
2. 示例代码
下面是一个简单的示例,展示了如何结合 Handler
和 ThreadLocal
进行消息传递:
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
public class HandlerThreadLocalExample {
// 静态的 ThreadLocal 变量,用于存储每个线程的 Handler 实例
private static final ThreadLocal<Handler> sThreadHandler = new ThreadLocal<>();
public static void main(String[] args) {
// 创建一个子线程
Thread thread = new Thread(() -> {
// 为当前线程准备 Looper
Looper.prepare();
// 创建一个 Handler 实例
Handler handler = new Handler(Looper.myLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println("Received message: " + msg.obj + " in thread: " + Thread.currentThread().getName());
}
};
// 将 Handler 实例存储到 ThreadLocal 中
sThreadHandler.set(handler);
// 发送消息
Message message = Message.obtain();
message.obj = "Hello from child thread";
handler.sendMessage(message);
// 启动 Looper 循环
Looper.loop();
});
thread.start();
}
}
在这个示例中,我们创建了一个静态的 ThreadLocal
变量 sThreadHandler
,用于存储每个线程的 Handler
实例。在子线程中,我们首先调用 Looper.prepare()
为当前线程准备 Looper
,然后创建一个 Handler
实例并将其存储到 sThreadHandler
中。接着,我们发送一条消息,并调用 Looper.loop()
启动 Looper
循环来处理消息。
总结
ThreadLocal
在 Handler
机制中起到了关键作用,它确保了每个线程都有自己独立的 Looper
和 Handler
实例,避免了多线程环境下的冲突和干扰。
2:子线程可以使用Handler吗?
答:子线程可以通过调用Looper.prepare准备资源,随后调用Looper.loop启动消息循环,消息循环启动后就可以正常的创建Handler,进行消息的发送和处理,当子线程没有消息需要处理时,则需要调用Looper.quit停止消息循环。
3:为什么子线程不可以直接new Handler而要先调用Looper.prepare
答:主线程在启动时,框架层默认执行了Looper.prepareMainLooper创建了消息队列等资源,并启动了消息循环,主线程的Looper循环是在ActivityThread.main函数内开启的,同样的,ActivityThread.main也是整个应用进程的入口,类似于java程序的main函数。子线程并没有默认开启的消息循环,所以需要我们手动调用Looper.prepare和Looper.loop
4:前面提到子线程调用Looper.prepare创建消息循环,Looper.prepare可以调用多次吗?Looper,MessageQueue,Handler,Thread,Message之间的数量关系是怎样的?
答:针对一个线程而言,其只能开启一个消息循环,也就意味着针对已经创建过消息循环的线程而言,再次调用Looper.prepare会抛出异常,同样的如果子线程没有调用Looper.prepare直接去使用Handler也会抛出异常,当子线程的Looper循环停止后,向其发送消息也会抛出异常。Looper,MessageQueue,Handler,Thread,Message之间的数量关系是Thread:Looper:MessageQueue:Handler:Message=1:1:1:N:N。
5:前面提到子线程主线程都可以创建Handler对象且有多个,那么怎么确定这个Handler的dispatchMessage在那个线程运行?Messae被处理时又是怎么确定是那个Handler对象响应呢?
答:对于Handler而言,其dispatchMessage方法运行在Looper关联的线程上,其实在Looper创建时,会将Looper对象和对应的线程绑定在一起。当Message被响应时,会通过Message.target对象确定消息的Handler。
Handler 机制的运行流程
消息发送流程
- 创建 Message 对象:可以通过
Message.obtain()
从消息池中获取可重用的 Message 对象,也可以直接new Message()
创建新的对象。 - 设置 Message 属性:例如设置
what
、arg1
、arg2
、obj
等字段,用于传递相关信息。 - 通过 Handler 发送消息:调用 Handler 的
sendMessage
、sendEmptyMessage
、sendMessageDelayed
等方法,这些方法会将 Message 对象发送到与 Handler 关联的 MessageQueue 中。以sendMessageDelayed
方法为例,其最终会调用sendMessageAtTime
方法,在该方法中会设置消息的执行时间when
,并调用enqueueMessage
方法将消息插入到 MessageQueue 中合适的位置。
消息处理流程
- Looper 循环取消息:Looper 的
loop
方法会进入一个无限循环,不断调用 MessageQueue 的next
方法从消息队列中取出消息。在next
方法中,会通过nativePollOnce
方法进行阻塞等待,直到有新消息进入队列或其他唤醒条件满足。 - 分发消息给 Handler:当 Looper 取出消息后,会调用消息的
target.dispatchMessage
方法,这里的target
就是发送该消息的 Handler,从而将消息分发到对应的 Handler 进行处理。 - Handler 处理消息:Handler 的
dispatchMessage
方法会根据消息的不同情况进行处理,最终可能会调用到我们重写的handleMessage
方法,在这里我们可以进行相应的业务逻辑处理,如更新 UI 等操作。
Handler 在不同线程中的使用及注意事项
主线程
在主线程中,Android 系统已经默认帮我们创建了 Looper 并启动了消息循环。我们可以直接创建 Handler 对象来发送和处理消息。例如,在 Activity 中常见的用法如下:
public class MainActivity extends AppCompatActivity {
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// 处理消息,更新UI等操作
TextView textView = findViewById(R.id.textView);
textView.setText("收到消息并更新UI");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 在子线程中模拟耗时操作
new Thread(() -> {
try {
Thread.sleep(2000);
Message message = Message.obtain();
message.what = 1;
mHandler.sendMessage(message);
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
}
子线程
在子线程中使用 Handler 时,需要手动调用Looper.prepare()
来创建 Looper 并关联 MessageQueue,然后创建 Handler 对象,最后调用Looper.loop()
启动消息循环。当子线程没有消息需要处理时,要记得调用Looper.quit()
或Looper.quitSafely()
停止消息循环,以避免资源浪费。示例代码如下:
class MyThread extends Thread {
private Handler mHandler;
@Override
public void run() {
Looper.prepare();
mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// 子线程中处理消息
Log.d("MyThread", "处理消息:" + msg.what);
}
};
// 模拟发送消息
Message message = Message.obtain();
message.what = 2;
mHandler.sendMessage(message);
Looper.loop();
}
}
然后在主线程中启动该子线程:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyThread myThread = new MyThread();
myThread.start();
}
}
6:Message在MessageQueue中是怎么组织的?怎么重用Message?MessageQueue是个队列吗?
答:MessageQueue虽然叫做Queue,但其实际上不是一个队列,MessageQueue实际上是一个以msg.what自小向大排序的头插单链表。我们可以通过Message.obtain来重用已经被处理的Message对象,这里实际也是通过一个单链表来实现消息重用的,头节点为sPool,由于sPool是静态变量,被线程共享,所以操作该链接时,使用了对象锁,重用的最大Message数量为50个(可以把这里的设计称之为消息池)
7:通过Handler.postDelayed发送的消息能准时执行吗?比如delay 50秒,50秒后一定响应吗?
答:通过Handler.postDelayed发送的消息不一定能准时执行,这主要是因为基准时间的问题,Handler.postDelayed的基准时间是SystemClock.uptimeMillis,该时间计算的是开机以来的非深度睡眠时间,也就意味着当我们设计的delay时间太长时,在这段时间如果发生了系统休眠,那休眠的这段时间是不算在delay时间内的,这样就与我们的真实时间不一致了。另外整体的消息执行是依附于线程的,而线程执行依赖于系统调度,当系统资源紧张时,线程调度自然也会延迟,进而导致延时消息无法按预期执行。
8:前面提到Handler是用来做线程间通信的,我在子线程通过主线程的Handler对象向主线程发送消息,是什么时间切换到主线程执行的?
答:前面也聊到了Thread,Looper,MessageQueue,Message,Handler之间的关系,当我们通过子线程向主线程发送消息时,该消息会被添加到主线程的MessageQueue中,至此子线程执行完成,消息的响应依赖于主线程Looper循环,当主线程Looper循环发现有消息需要处理时,则会将处理消息,调用Message.target.dispatchMessage方法进行处理,该过程发送在主线程中,通过主线程向子线程发消息也是类似的道理。
9:通过子线程向主线程消息队列发送消息不会有多线程问题吗?
答:不会的,在MessageQueue中,消息进队时,使用了synchronized+this锁,来确保多线程环境安全。
Handler 机制的应用场景及优势
应用场景
- UI 更新:这是最常见的应用场景。在子线程中完成网络请求、数据读取等耗时操作后,通过 Handler 将结果传递回主线程,然后在主线程中更新 UI,确保 UI 的流畅性和响应性。
- 定时任务:通过
Handler.postDelayed
方法可以实现定时任务,例如实现一个倒计时功能。 - 线程间协作:多个线程需要相互协作完成复杂任务时,Handler 可以作为线程间通信的桥梁,实现数据在不同线程之间的传递和交互。
优势
- 线程安全:通过将 UI 更新操作放在主线程中执行,避免了多线程并发访问 UI 带来的线程安全问题。
- 解耦:将耗时操作和 UI 更新等操作分离,使得代码结构更加清晰,各部分职责明确,增强了代码的可维护性和可扩展性。
- 灵活的消息处理:可以根据不同的消息类型和参数,在
handleMessage
方法中进行灵活的业务逻辑处理,满足各种复杂的需求。
总结
Handler 机制是 Android 开发中线程间通信的重要手段,深入理解其源码级机制对于编写高效、稳定的 Android 应用至关重要。从 Message、MessageQueue、Looper 到 Handler,它们相互协作,共同完成了消息的发送、存储、循环取出和处理的过程。在不同线程中正确使用 Handler,合理利用其应用场景和优势,能够有效提升应用的性能和用户体验。希望通过本文的深入剖析,能帮助开发者更好地掌握 Handler 机制,在 Android 开发中更加游刃有余。