Dart single thread description

In Java, single-threaded execution is the default, with the main() method running the code sequentially.

Dart’s single-threaded approach is completely different from Java’s. Although both are single-threaded, Dart comes in three forms:

  • The main thread
  • Microtask queue
  • Event task queue

The main thread

Like Java, it has uniqueness, that is, threads that start from main().

Microtask queue

It contains many microtasks, which are scheduled mainly through scheduleMicrotask.

Event task queue

It contains many event tasks, such as I/O event tasks and Timer event tasks.

Dart Single-thread priority

Main thread > Microtask Queue > Event task Queue.

So, in the Dart single-threaded, priority after the main thread, in the process of the implementation of the main thread, if discover micro task or event task, will put them into the corresponding task queue, then one by one the inside of the execution of the micro task queue tasks, secondly to events in the task queue missions.

However, there are special cases where event tasks can be generated in microtasks, and microtasks can be generated in event tasks. In this case, how do they perform?

Microtasks generate event tasks

Just said that the execution of the main thread before executing the task queue, however, produced the event task when executing the task, when will the task to join the events task queue, because of the micro task queue priority is higher than the task of events, so, still can be carried first performed micro task queue again event task queue.

Event tasks generate microtasks

Since the priority of the microtask queue is higher than that of the event task queue, the microtask queue is empty when it is executed to the event task queue, but the microtask is generated in the event task queue. Therefore, whether the microtask queue is empty will be judged after each execution of the current event task:

  • The microtask queue is empty and continues to execute the next event task.
  • If the microtask queue is not empty, it will execute the microtask. After the completion of the microtask, it will judge whether the microtask queue is empty again and execute the event task queue only when the microtask queue is not empty.

Code validation

Special instructions

Because the code runs directly in the Flutter project, it will be deleted when the code is posted

  • import 'package:flutter/material.dart';.
  • runApp(MyApp());Relevant code.

Keep only the core code.

Base code Sample

First, let’s show the simplest example, which has only one main thread, one microtask, and one event task.

import 'dart:async'; Void main() {print('main start! '); // scheduleMicrotask((){print(' scheduleMicrotask! '); }); Timer.run(() {print(' Event task executed! '); }); print('main end! '); }Copy the code

Running results:

I/flutter: main start! I/flutter: main end! I/ FLUTTER: Microtask execution! I/ FLUTTER: Event task execution!Copy the code

Extended code example

What follows is an example of generating event tasks in microtasks and generating microtasks in event tasks, in addition to the basic code example above.

import 'dart:async'; Void main() {print('main start! '); // scheduleMicrotask((){print(' start! '); // Subevent task timer.run (() {print(' Subevent task execution! '); }); Print (' end! '); }); Run (() {print(' start! '); ScheduleMicrotask ((){print(' scheduleMicrotask is executed! '); }); Print (' Event task end! '); }); print('main end! '); }Copy the code

Running results:

I/flutter: main start! I/flutter: main end! I/ FLUTTER: microtask start! I/ FLUTTER: Microtask end! I/ FLUTTER: event task start! I/ FLUTTER: Sub-microtask execution! I/ FLUTTER: Subevent task execution!Copy the code

congestion

Although it can be seen from the above results that in a single thread, the main thread, the microtask queue, and the event task queue all run linearly, if we add blocking operations in them, will they still run linearly? Or, actually, the task queue is concurrent, but the priority of the microtask queue is slightly higher than that of the event task queue.

Here, we add sleep(Duration(seconds: 2)); Code emulates time-consuming operations.

import 'dart:async'; import 'dart:io'; Void main() {print('main start! '); sleep(Duration(seconds: 2)); // scheduleMicrotask((){print(' start! '); sleep(Duration(seconds: 2)); // Subevent task timer.run (() {print(' Subevent task execution! '); sleep(Duration(seconds: 2)); }); Print (' end! '); }); Run (() {print(' start! '); sleep(Duration(seconds: 2)); ScheduleMicrotask ((){print(' scheduleMicrotask is executed! '); sleep(Duration(seconds: 2)); }); Print (' Event task end! '); }); print('main end! '); }Copy the code

Running results:

I/flutter: main start! I/flutter: main end! I/ FLUTTER: microtask start! I/ FLUTTER: Microtask end! I/ FLUTTER: event task start! I/ FLUTTER: Sub-microtask execution! I/ FLUTTER: Subevent task execution!Copy the code

The result remains the same, indicating that the main thread, microtask queue, and event task queue execute linearly in the Dart single thread.

Guess you like

  • Interviewer: How can Message be prioritized
  • Make an App that never crashes
  • Custom Gradle Plugin+ bytecode staking
  • From handwritten ButterKnife to mastering annotations, AnnotationProcessor
  • Here, I want you to write a performance check

It’s not easy, and your likes are my biggest support.