12

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.

Community
  • 1
  • 1
user316117
  • 7,971
  • 20
  • 83
  • 158

2 Answers2

6
  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

Budius
  • 39,391
  • 16
  • 102
  • 144
  • On #4 I agree that, being a Queue, it's probably a FIFO but I'm unclear on WHEN something is executed. For example, when you call _notifyDataSetChanged()_ it will schedule a layout pass, and I assume the event quese is involved in this. But when will that occur WRT to whatever else you're doing "at the same time" in the UI thread? In other words, what triggers the system to grab the next item off the Event Queue? – user316117 May 28 '14 at 17:10
  • as I never dig deep on it I can only imagine and guess. My best guess is that there're 2 main elements that dispatch events to the UI thread. Views/Drawables/etc (because they want to draw), using invalidate, requestLayout, notifyDataSetChanged, etc. And the touch interface driver that creates touch events and dispatch to the Window. And as said before, those are all queue in a FIFO. And that's the whole reason devs must not block the UI thread and never do stuff that takes more than a couple milisecond, specially during scroll. All those events would be blocked in the queue. – Budius May 28 '14 at 19:35
  • and to answer that last question: The trigger for the system to grab next item on the EventQueue is the fact that the `EventQueue.size() > 0;` that makes the Thread to wake up and enter the scheduler. The scheduler is (probably) the Linux scheduler http://en.wikipedia.org/wiki/Completely_Fair_Scheduler and here Dianne Hackborn makes a nice discussion on thread priority, scheduling: https://plus.google.com/u/0/105051985738280261832/posts/XAZ4CeVP6DC – Budius May 28 '14 at 19:41
3

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.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127