2011-12-01 58 views
2

我的Android应用程序中的非UI线程包含Handler对象,并使用post()方法将Runnable的实现添加到此Handler。Handler是否同时执行Runnable

从阅读API看来,Handler似乎使用了一个队列。这是否意味着如果我将()2个Runnables发布到队列中,那么第二个只能在完成第一个Runnable的运行方法时才开始执行?

只是想确认一下,因为我在我的应用程序中看到了一些多线程问题。

感谢

回答

2

(按我理解你的问题)

按照该API,处理器使用的MessageQueue,第二运行执行第一完成后开始,

一个处理程序允许您发送和处理与线程的MessageQueue关联的Message和Runnable对象。每个Handler实例都与单个线程和该线程的消息队列相关联。当您创建一个新的处理程序时,它将绑定到正在创建它的线程的线程/消息队列 - 此后,它将消息和可运行消息传递到该消息队列,并在消息出来时执行它们队列。

处理程序有两个主要用途:(1)安排将来要执行的消息和可运行子程序;和(2)排队在不同于你自己的线程上执行的动作。

1
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. 

引用自:http://developer.android.com/reference/android/os/Handler.html 涉及到你的问题,它是YES,后一日的Runnable做第二人会跑。