手写Handler流程
Looper
public class Looper {
public static final ThreadLocal<Looper> mThreadLocal = new ThreadLocal<>();
/**
* 消息队列
*/
public MessageQueue mQueue;
private Looper() {
mQueue = new MessageQueue();
}
/**
* 初始化Looper,保证一个线程只有一个Looper对象
*/
public static void prepare() {
if (mThreadLocal.get() != null) {
throw new RuntimeException("Only one Looper may be created per thread");
}
mThreadLocal.set(new Looper());
}
/**
* 开启轮询
*/
public static void loop() {
// 通过myLooper静态方法,最终将非静态的mQueue变量取的
final Looper me = myLooper();
final MessageQueue queue = me.mQueue;
while (true) {
Message msg = queue.next();
if (msg != null) {
msg.target.dispatchMessage(msg);
}
}
}
/**
* 从ThreadLocal取的本线程的Looper
*
* @return
*/
public static Looper myLooper() {
return mThreadLocal.get();
}
}
Message
public class Message {
public Handler target;
public Object obj;
@Override
public String toString() {
return "Message{" +
"target=" + target +
", obj=" + obj +
'}';
}
}
MessageQueue
/**
* 消息队列
*/
public class MessageQueue {
/**
* 阻塞队列
*/
private BlockingQueue<Message> queue = new ArrayBlockingQueue<>(50);
/**
* 从阻塞队列中取出消息
*
* @return
*/
public Message next() {
try {
return queue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
/**
* 将消息存入阻塞队列
*
* @param msg
*/
public void enqueueMessage(Message msg) {
try {
queue.put(msg);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
Handler
public class Handler {
private MessageQueue mQueue;
private Looper mLooper;
public Handler() {
mLooper = Looper.myLooper();
if (mLooper == null) {
throw new RuntimeException(
"Can't create handler inside thread " + Thread.currentThread()
+ " that has not called Looper.prepare()");
}
// 将Looper的队列赋值到全局变量mQueue中
mQueue = mLooper.mQueue;
}
/**
* 发送消息
*
* @param msg
*/
public void sendMessage(Message msg) {
enqueueMessage(msg);
}
/**
* 将消息放到消息队列中
*
* @param msg
*/
private void enqueueMessage(Message msg) {
// 设置msg的target为本对象
msg.target = this;
mQueue.enqueueMessage(msg);
}
/**
* 提供给子类的处理消息的方法
*
* @param msg
*/
public void handleMessage(Message msg) {
}
/**
* 处理消息
*
* @param msg
*/
public void dispatchMessage(Message msg) {
handleMessage(msg);
}
}
测试代码
public class ActivityThread {
@Test
public void main() {
Looper.prepare();
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
System.out.println(msg.obj.toString());
System.out.println("从主线程处理消息msg" + msg.toString() + ", 线程名: " + Thread.currentThread().getName());
}
};
new Thread() {
@Override
public void run() {
Message msg = new Message();
msg.obj = "Hello , Handler";
System.out.println("从子线程发送消息msg" + msg.toString() + ", 线程名: " + Thread.currentThread().getName());
handler.sendMessage(msg);
}
}.start();
Looper.loop();
}
}
打印结果
从子线程发送消息msgMessage{target=null, obj=Hello , Handler}, 线程名: Thread-0
Hello , Handler
从主线程处理消息msgMessage{target=com.sanguine.myhandler.ActivityThread$1@722c41f4, obj=Hello , Handler}, 线程名: main
Process finished with exit code -1
2265

被折叠的 条评论
为什么被折叠?



