简体   繁体   English

事件循环何时在 Dart 中开始以及事件队列如何工作

[英]when the event loop starts in Dart and how the event queue works

The first question is when the event loop starts?第一个问题是事件循环何时开始? I read in a site that it's start after the main method but why when we try something like this我在一个网站上读到它是在 main 方法之后开始的,但是为什么当我们尝试这样的事情时

main()async {
Future(()=>print('future1'));
await Future(()=>print('future2'));
print('end of main');
}

//the output is :
//future1
//future2
//end of main

in this example the event loop start when we use the await keyword and after the event loop reaches the future2 it's paused?在这个例子中,当我们使用 await 关键字时,事件循环开始,在事件循环到达 future2 之后,它被暂停了吗? or i am wrong:(或者我错了:(

The second question is how the events is added to event queue if it's FIFO why in this example the future 2 is completed before future 1第二个问题是事件如何添加到事件队列,如果它是先进先出的,为什么在这个例子中,未来 2 在未来 1 之前完成

main(){
Future.delayed(Duration(seconds:5) , ()=>print('future1'));
Future.delayed(Duration(seconds:2) , ()=>print('future2')); 
}

The event loop run when there is nothing else running ( eg main method is done, you are waiting for some future to complete ).事件循环在没有其他东西运行时运行(例如 main 方法已完成,您正在等待某个未来完成)。

Your example makes sense because the first line puts an event on event queue so now the first item in the queue is "print('future1')".您的示例很有意义,因为第一行将事件放在事件队列中,所以现在队列中的第一项是“print('future1')”。 In the next line, you are putting another event on the queue which calls "print('future2')" and now you await for this event to be done.在下一行中,您将另一个事件放在调用“print('future2')”的队列中,现在您等待该事件完成。

Since your main method is not waiting for something then the event loop is going to be executed.由于您的 main 方法没有等待某些东西,因此将执行事件循环。 Since the first event on the queue was "print('future1')" then this is going to be executed first.由于队列中的第一个事件是“print('future1')”,那么这将首先执行。 But since the main method is still waiting for the future "print('future2')" to be complete then the event loop takes another event to be executed which are going to be "print('future2')".但由于主要方法仍在等待未来的“print('future2')”完成,因此事件循环需要执行另一个事件,该事件将是“print('future2')”。

Since this event was the one the main method was waiting for (and there is no more event on the event queue) then main() are going to run the last call "print('end of main')".由于此事件是 main 方法正在等待的事件(并且事件队列中没有更多事件),因此 main() 将运行最后一次调用“print('end of main')”。

In your next example, you think that Future and Future.delayed are the same which it is not.在您的下一个示例中,您认为 Future 和 Future.delayed 是相同的,但事实并非如此。 With Future.delayed there are not going any event in the event queue before.使用 Future.delayed 之前,事件队列中没有任何事件。 Instead, there are running a thread outside the VM which knows when the next timer should trigger which ends up putting an event on the queue.相反,在 VM 外部运行一个线程,它知道下一个计时器何时应该触发,最终将一个事件放在队列中。 So the event is only being put on the event queue when the timer has been expired (and therefore, the future2 are going to be executed first).因此,只有当计时器到期时,事件才会被放入事件队列中(因此,future2 将首先执行)。

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

相关问题 浏览器中的事件循环如何同时处理事件队列、作业队列、渲染队列? - How does the event loop in the browser deal with the event queue, job queue, render queue at the same time? 事件循环,回调队列和Javascript的单线程如何连接? - How are the Event Loop, Callback Queue, and Javascript’s single thread connected? Javascript事件循环任务队列可能溢出吗? - Is Javascript event loop task queue overflow possible? 什么时候 onDone 事件对 Dart 中的 StreamSubscription 起作用? - When does onDone event work for StreamSubscription in Dart? 事件循环更喜欢微任务队列而不是回调队列? - event loop prefers microtask queue over callback queue? 将回调直接排入事件队列 - Queue a callback directly into an event queue 事件队列清理 - Event queue cleanup C#“事件循环”与JavaScript“事件循环”相比如何? - How does the C# “event loop” compare to the JavaScript “event loop”? 引入事件循环优先于任务队列的单独微任务队列的动机是什么? - What was the motivation for introducing a separate microtask queue which the event loop prioritises over the task queue? 事件溯源:我什么时候(而不是)应该使用 Message Queue? - Event-sourcing: when (and not) should I use Message Queue?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM