简体   繁体   English

运行循环如何工作?

[英]How does a Run Loop work?

Out a curiosity, how does a Run Loop work? 出于好奇,Run Loop如何工作? The official docs say 官方文件

The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none. 运行循环的目的是在有工作要做时让线程忙,而在没有工作时让线程进入睡眠状态。

How is this implemented efficiently? 如何有效实施? Is the run loop actually a loop that is constantly running? 运行循环实际上是不断运行的循环吗? ie. 即。 while(true) ? while(true)吗?

I understand it as something waiting for inputs to come to do things. 我将其理解为等待输入操作的东西。

For example, when you use your phone, the screen is displaying things but at the same time it is waiting for any inputs to come do stuff. 例如,当您使用手机时,屏幕上显示的是事物,但与此同时它正在等待任何输入信息。 Inputs can be you touching the screen, someone sending you a text, some scheduled alarm ready to ring, etc. 输入可以是您触摸屏幕,有人向您发送文本,一些预定的警报准备响起等。

Thus using a run loop would be describing all the accepted inputs (events, schedule, etc.) with handlers, and when that is done, start waiting for those inputs to come. 因此,使用运行循环将与处理程序一起描述所有接受的输入(事件,时间表等),完成后,开始等待这些输入到来。

The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none. 运行循环的目的是在有工作要做时让线程忙,而在没有工作时让线程进入睡眠状态。

This means that every time your loop is waiting for inputs, concerned thread will be idle, and once an input comes, the thread will be active and execute the stuff it needs to. 这意味着每次循环等待输入时,相关线程将处于空闲状态,一旦输入进入,线程将处于活动状态并执行所需的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM