I can hear you saying “Wait, what ? Dart is single-threaded ?” In fact, Dart provides isolates, which are, in Dart’s word, some kind of multithreading. But not really…. We’ll talk about it later, let’s dive in the event loop concept first, assuming we excecute our Dart code in the main isolate, that is single-threaded.
As a good single-threaded language, Dart executes only one operation at a time. This means that as long it’s executing something, the next operations coming in the queue have to wait for it to end. So, when Dart is executing a lenthy task, it might possibly block the thread, and the app doesn’t respond to user interactions anymore until the lengthy task is performed. This could be quite annoying right ?
The Queues and the loop
In this single-threaded isolate, Dart provides 2 queues : one for the microtasks, another for the events. The event loop is a sort of infinite loop that runs all the time to watch at Dart’s microtasks and events queues. Both queues are First in, first out. FIFO is a method for organising the manipulation of a data structure (often, specifically a data buffer) where the oldest (first)... (first-in, first-out), so the first thing to come inside the queue will be the first one to get out.
The event loop watches for microtasks in higher priority. If the microtasks queue is not empty, it execute the first microtask and the next until there is no more microtasks in the queue. Then, when the microtasks queue is empty, it watches at the event queue and execute the first event. After executing the first event, the event loop watches again in the microtasks queue, and if it’s empty, it processes another event, and watches first the microtasks, and events, and micr… ok you get it? This is an infinite loop watching the microtasks, and then, the events. And again.
Things you need to remember are :
- Dart comes in an isolate, which provides a single thread.
- This thread comes with its own piece of memory, a Dart Runtime is the period of time when a program is running. It begins when a program is opened (or executed) and ends with the program is quit or closed..., two First in, first out. FIFO is a method for organising the manipulation of a data structure (often, specifically a data buffer) where the oldest (first)... queues (one for microtasks, one for events) and useful stuff like an event loop or a powerful garbage collector.
- Event loop watches the queues all the Runtime is the period of time when a program is running. It begins when a program is opened (or executed) and ends with the program is quit or closed... long.
- Microtasks have higher priority than events.
This is kinda hard to explain the event loop with written words, but I found a great video that shows this process very clearly, and helps a lot to understand what’s happening here :
I hope this makes Event loop easier to understand for you, as it’s a really important concept for your application performances.
In the next post, I’ll talk about the Dart isolates. Stay tuned !