The Fundamentals of Zones, Microtasks and Event Loops in the Dart Programming Language - Dart Tutorial Part 3

dart-logo.png

Repository

https://github.com/dart-lang/sdk

What Will I Learn?

  • You will learn what a Zone is in Dart
  • You will learn how Microtasks work
  • You will learn about the Event loop
  • You will learn about the Event Queue and Microtask Queue
  • You will learn how Futures interact with Zones and the Event Loop

Requirements

System Requirements:
OS Support for Dart:
  • Windows 7 SP1 or later (64-bit)
  • macOS (64-bit)
  • Linux (64-bit)

Required Knowledge

  • Some knowledge of basic programming
  • Some understanding asynchronous execution
  • A fair understanding of development and Imperative or Object Oriented Programming

Resources for Flutter and this Project:

Sources:

Dart Logo: https://www.dartlang.org/

Difficulty

  • Intermediate

Description

Outline and Overview

In this Dart Video Tutorial, we take a look at some of the building block concepts of Dart in the form of Zones and the Event Loop. We look at Microtasks and how they interact with Zones. We also look at how futures work in relation to Zones and the Event Loop. We talk about how there are two queues; the Event Queue and the Microtask Queue and we talk about the implications of this on the Dart language.

Planning Events on a Single Thread

Dart is a single threaded programming language. Because of this, the language makes use of an event loop to schedule the execution of pieces of code. The event loop takes an item from the event queue and handles it. It repeats these steps over and over again until there are no more items in the queue. Generally, these events represent user input or output, timers and other side effects.

event_loop.png

In the chart above, we can see how the Task queue and microtask queues work together. The microtask queue is created the first time a microtask is scheduled and it only exists for as long as a microtask exists. The event queue or task queue exists from the start of the program to the end of execution. Futures, timers and other asynchronous processes move through this portion of the program.

Task order and its implications.

Once a program starts to execute in dart, the main function is executed. The program, checks to see if there is a microtask scheduled at any point during the execution of the application. If this comes back as true, then those microtasks are scheduled in the microtask loop and then executed after all of the top level code is executed. There is a check for Events/Tasks next and this then forces these events into the event loop. These events have the final order priority in line.

out_order.png

This image shows the order of an application that uses all three different execution types. First, we see the normal top level execution functions which gives us Top of function and Bottom of Main Function. The callbacks for the Microtasks are executed next with first microtask and Second microtask being the output for these two functions. Finally, we see the events execute through the two futures. Future Two executes first because Future one is a delayed future.

Zoning out a Single Thread.

On a very low level, dart uses Zones do decide execution order. Zones are the Asynchronous Dynamic Extent of a call. Zones are similar to NodeJs' domains or Java's thread-local storage. It is the computation that is preformed during execution and the asynchronous callbacks that are registered by the code. The compiler decides at which point to create a new zone based on the size of a task. We can also manually deal with zones directly which allows us to protect our applications from errors and override various basic features.

zoned.png

In this code block, we use the runZoned function to create a new zone. In this zone, we have a stream feeding numbers to a print function. If the print function hits the number 9 then an error is automatically thrown out. Because this is happening in the child zone of the root zone, the application is allowed to continue executed even after this error happens. Also, we are able to overload the print functionality of the zone which allows us to attach a timestamp to the front of each of the print statements in the zone.

The source code for this project can be found here

Video Tutorial

Curriculum

Proof of Work Done

https://github.com/tensor-programming

H2
H3
H4
3 columns
2 columns
1 column
12 Comments