想了解更多关于Android UI线程的事件队列的信息

时间:2021-12-13 00:01:54

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?

整个web和堆栈溢出都有对UI线程的事件队列的引用。例如,runOnUiThread()将向UI线程的事件队列发送一个动作。但是我还没有找到这个队列的详细描述,所以谁能给我一个详细的描述,或者回答几个问题?

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?

1。我知道它是一个队列,它包含“动作”,但我不太清楚什么是“动作”。操作方法是否调用它们的相关参数,或者是对线程本身的指令,还是什么?

2. Do all threads have event queues or just the UI thread?

2。所有线程都有事件队列还是只有UI线程?

3. How can I see what's in the Event Queue, or get a count of events?

3所示。如何查看事件队列中的内容,或获取事件计数?

4. What exactly determines when an action in the queue is executed?

4所示。什么确切地决定了何时执行队列中的操作?

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?

5。视图类有一个名为cancelPendingInputEvents()的方法,该方法用于“取消先前发布到事件队列的任何延迟高级输入事件”。如果事件队列是线程的属性,为什么这是视图类的方法,或者视图有一些不同的事件队列?

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.

6。消息队列和事件队列是两个不同的队列吗?注意:有人在这里问过这个问题,回答者一开始说他们是同义的,后来又加上了一个附录,似乎暗示信息不同,所以我不清楚最终的答案是什么。

2 个解决方案

#1


5  

  1. it's a queue with Runnables. The thread calls run(); on each of the runnables.
  2. 这是一个可运行的队列。线程调用run();在每个runnables。
  3. 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()"
  4. 只有调用Looper.prepare()的线程,因此任何线程都可能拥有它们。有一个运行时异常,“不能在线程中创建一个不调用Looper.prepare()的处理程序。”
  5. 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.
  6. 你不能。内容由平台管理,调用活动回调、片段回调、分派触摸事件、运行动画、运行布局、测量和绘制。所有这些都在UI线程中。
  7. AFAIK it's a FIFO. But I might be wrong on that one.
  8. 这是一个先进先出的。但我可能错了。
  9. 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)
  10. 视图有一个UI线程的处理程序。处理程序绑定到线程,它是MessageQueue。这就是通过在UI线程上调用new handler()创建新的UI线程处理程序的方法。然后通过调用handler.post(Runnable)将内容发送到该线程队列
  11. I don't believe they're different. But would have to dig on source code to be sure.
  12. 我不相信它们是不同的。但是要想确定的话,就必须挖掘源代码。

It's always helpful to read the docs:

阅读文档总是很有帮助的:

https://developer.android.com/reference/android/os/Handler.html

https://developer.android.com/reference/android/os/Handler.html

https://developer.android.com/reference/android/os/MessageQueue.html

https://developer.android.com/reference/android/os/MessageQueue.html

#2


2  

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:

它只是一个标准的消息循环,就像每个GUI平台使用的那样。“事件”是一个CS术语,而不是一个特定的对象。想象一下,在安卓框架内你会看到这样的东西:

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.

只有UI线程有一个事件循环,尽管您可以在另一个线程上编写自己的事件循环。

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.

消息队列和事件队列是一回事。还有一个名为MessageQueue的类,它与我们在这里讨论的队列不同,但是它可以用来实现一个。

#1


5  

  1. it's a queue with Runnables. The thread calls run(); on each of the runnables.
  2. 这是一个可运行的队列。线程调用run();在每个runnables。
  3. 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()"
  4. 只有调用Looper.prepare()的线程,因此任何线程都可能拥有它们。有一个运行时异常,“不能在线程中创建一个不调用Looper.prepare()的处理程序。”
  5. 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.
  6. 你不能。内容由平台管理,调用活动回调、片段回调、分派触摸事件、运行动画、运行布局、测量和绘制。所有这些都在UI线程中。
  7. AFAIK it's a FIFO. But I might be wrong on that one.
  8. 这是一个先进先出的。但我可能错了。
  9. 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)
  10. 视图有一个UI线程的处理程序。处理程序绑定到线程,它是MessageQueue。这就是通过在UI线程上调用new handler()创建新的UI线程处理程序的方法。然后通过调用handler.post(Runnable)将内容发送到该线程队列
  11. I don't believe they're different. But would have to dig on source code to be sure.
  12. 我不相信它们是不同的。但是要想确定的话,就必须挖掘源代码。

It's always helpful to read the docs:

阅读文档总是很有帮助的:

https://developer.android.com/reference/android/os/Handler.html

https://developer.android.com/reference/android/os/Handler.html

https://developer.android.com/reference/android/os/MessageQueue.html

https://developer.android.com/reference/android/os/MessageQueue.html

#2


2  

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:

它只是一个标准的消息循环,就像每个GUI平台使用的那样。“事件”是一个CS术语,而不是一个特定的对象。想象一下,在安卓框架内你会看到这样的东西:

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.

只有UI线程有一个事件循环,尽管您可以在另一个线程上编写自己的事件循环。

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.

消息队列和事件队列是一回事。还有一个名为MessageQueue的类,它与我们在这里讨论的队列不同,但是它可以用来实现一个。