美文网首页
Looper、Handler 、HandlerThread

Looper、Handler 、HandlerThread

作者: blue_lights | 来源:发表于2021-09-23 09:45 被阅读0次

Handler的主要用途

to schedule messages and runables to be excuted as some point in the future 

 1、未来某时做某事 

 to enqueue an action to be performed on a different thread than you own 

 2、线程间通信


MessageQueue

 是一个队列,其中包含应处理的称为消息的任务。

MessageQueue is a queue that has tasks called messages which should be processed.

Handler

Handler enqueues task in the MessageQueue using Looper and also executes them when the task comes out of the MessageQueue.

Handler使用Looper入队任务,并在出队列时执行任务、

Looper 

Looper is a worker that keeps a thread alive, loops through MessageQueue and sends messages to the corresponding handler to process.

Looper是一个工作线程,它使线程保持活动状态,循环遍历MessageQueue并将消息发送到相应的handlerto 进程。

Finally Thread gets terminated by calling Looper’s quit() method.

最后Thread通过调用 Looper 的quit()方法终止。

一个线程只能有一个唯一的 Looper,并且可以有许多与之关联的唯一处理程序



一个简单代码示例:

相关文章

网友评论

      本文标题:Looper、Handler 、HandlerThread

      本文链接:https://www.haomeiwen.com/subject/mfvngltx.html