All over the web and on Stack Overflow there are references to the UI Thread's Event Queue. For example runOnUiThread() will post an action to the UI thread's Event Queue. But I haven't been able to find a detailed description of this queue, so could someone please point me to a detailed one, or answer a few questions?
1. I get that it's a queue and that it contains "actions", but I'm a little unclear what an "action" is. Are actions method calls with their associated parameters, or instructions to the thread itself, or what?
2. Do all threads have event queues or just the UI thread?
3. How can I see what's in the Event Queue, or get a count of events?
4. What exactly determines when an action in the queue is executed?
5. The View class has a method called cancelPendingInputEvents() which is used to "Cancel any deferred high-level input events that were previously posted to the event queue." If the event queue is a property of a thread, why is this a method of the View class, or do views have some different Event Queue?
6. Are the message queue and event queue two different queues? N.B. - someone asked this on SO here and the answerer started by saying they were synonymous and then appended an addendum which seemed to imply messages were different so I'm unclear what the final answer was.
EventQueue is a platform-independent class that queues events, both from the underlying peer classes and from trusted application classes.
User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.
Android has four basic types of threads. You'll see other documentation talk about even more, but we're going to focus on Thread , Handler , AsyncTask , and something called HandlerThread . You may have heard HandlerThread just called the “Handler/Looper combo”.
Main Thread: The default, primary thread created anytime an Android application is launched. Also known as a UI thread, it is in charge of handling all user interface and activities, unless otherwise specified. Runnable is an interface meant to handle sharing code between threads. It contains only one method: run() .
run();
on each of the runnables.Looper.prepare()
, so any thread can potentially have them. There's an Runtime Exception for that "Can't create handler inside thread that has not called Looper.prepare()" new Handler()
on the UI thread. And then post stuff to that thread queue by calling handler.post(Runnable)
It's always helpful to read the docs:
https://developer.android.com/reference/android/os/Handler.html
https://developer.android.com/reference/android/os/MessageQueue.html
It's just a standard message loop, like every GUI platform uses. "Event" is a CS term, not a particular object. Imagine that inside the Android framework you'd see something like this:
MessageQueue queue;
void run(){
while(1){
queue.waitForEvent();
Message msg = queue.getEvent();
//Handle msg
}
}
Only the UI thread has an event loop, although you could write your own on another thread.
You cannot see the event queue or get a list of events. The ones you need to know about will call some function in your code
Events are executed as soon as the thread can. If there are no events in the queue, the thread sleeps. They should be executed in order, although the framework may cheat on some events.
A message queue and event queue are the same thing. There's also a class called MessageQueue, which is not the same as the queue we're talking about here but which may be used to implement one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With