Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Want to understand more about Android UI Thread's Event Queue

Tags:

android

events

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.

like image 918
user316117 Avatar asked May 28 '14 16:05

user316117


People also ask

What is an event queue?

EventQueue is a platform-independent class that queues events, both from the underlying peer classes and from trusted application classes.

What is the purpose of a UI thread?

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.

How many types of threads are there in Android?

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

What is main UI thread in Android?

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() .


2 Answers

  1. it's a queue with Runnables. The thread calls run(); on each of the runnables.
  2. only threads that called 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()"
  3. You can't. Stuff is managed by the platform and calls Activity callbacks, Fragment callbacks, dispatch touch events, run animations, run layout, measure and draw. All this in the UI thread.
  4. AFAIK it's a FIFO. But I might be wrong on that one.
  5. Views have a Handler to the UI thread. Handlers are bound to the thread and it's MessageQueue. That's how you can create a new UI thread handler by calling new Handler() on the UI thread. And then post stuff to that thread queue by calling handler.post(Runnable)
  6. I don't believe they're different. But would have to dig on source code to be sure.

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

like image 151
Budius Avatar answered Oct 07 '22 06:10

Budius


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.

like image 35
Gabe Sechan Avatar answered Oct 07 '22 07:10

Gabe Sechan