android - Handler是否同时执行Runnable

标签 android

我的 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/

相关文章:

android - android中同一应用程序的多个实例

android - 使用 com.flyingsoftgames.xapkreader 插件扩展 Phonegap APK

Android - 未连接 Eclipse 时记录日志

android - 有没有办法在模拟器中运行玻璃器皿?

android - 我可以在一个页面上放置两个 AdMob 广告(固定横幅广告和可滚动原生广告)

java - 数据报 channel 套接字不编写 Java

android - 不同的 Android SDK 版本使用不同的图标

android - 伴随对象 - Android 是否希望将它们注册为 Activity (?)

java - Android : sending data to a WebView with loadUrl or retrieving it from the WebView with a JavascriptInterface? 中什么更快

android - Android Studio中gradle项目手动下载超时的依赖文件