了解与 Handler 和 AsyncTask 关联的线程

Understanding threads associated with Handler and AsyncTask

AsyncTask must be created and executed from the Main thread and it runs on Worker thread. However, Main thread methods may be invoked in between to publish progress.

那么处理程序呢?可以从任何线程创建处理程序吗?处理程序在哪个线程上运行?在线程方面,处理程序与异步任务有何不同?

What is the difference between AsyncTask and Handlers and which one would be better to use in Listview? ..这里提到-The Handler is associated with the application’s main thread. it handles and schedules messages and runnables sent from background threads to the app main thread...如果处理程序在主线程上运行,那么它如何从后台线程调度消息?

主线程是 UI 更新的线程。工作线程是除主线程之外的任何线程。是的,处理程序可以在任何线程中创建,并且它与创建它的线程相关联。参见 Handler

来自documentation

When you create a new Handler, it is bound to the thread / message queue of the thread that is creating it -- from that point on, it will deliver messages and runnables to that message queue and execute them as they come out of the message queue.

这意味着您 运行 使用处理程序的所有内容都将 运行 在创建处理程序的那个线程中。

为了更好地理解阅读这篇文章:Processes and Threads