我的 Android 应用程序中的非 UI 线程包含一个 Handler 对象,我使用 post() 方法将 Runnable 的实现添加到该 Handler 中。
从API来看,Handler似乎使用了队列。这是否意味着如果我将 2 个 Runnable 发布到队列中,第二个 Runnable 只会在第一个 Runnable 的 run 方法完成后开始执行?
只是想确认这一点,因为我在我的应用程序中发现了一些多线程问题。
谢谢
最佳答案
(据我了解你的问题)
根据 API reference ,使用 MessageQueue 的处理程序,第二个可运行执行在第一个执行完成后开始,
A Handler allows you to send and process Message and Runnable objects associated with a thread's MessageQueue. Each Handler instance is associated with a single thread and that thread's message queue. 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.
There are two main uses for a Handler: (1) to schedule messages and runnables to be executed as some point in the future; and (2) to enqueue an action to be performed on a different thread than your own.
关于android - Handler是否同时执行Runnable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8336579/