First, the last figure

First, it performs all microtasks in FIFO (first-in, first-out) order. It then dequeues and processes the first item in the event queue, and then repeats the cycle: performing all the microtasks, and then processing the next event on the event queue.

Key points:

1. By default, microtask queues only have events for dart core code

2. When the event loop is executing a task in the microtask queue, the event queue gets stuck: the application can’t draw a graph, handle a mouse click, react to I/O, etc. (so this asynchronous implementation can also cause the page to stall)

3. Latency under this implementation is error-free, and when you create a delayed task, events will be queued at the time you specify. It still waits for all events in the event queue before it (including every event in the microtask queue) to complete before it can be executed without cutting the queue

The Future and scheduleMicrotask ()

The Future class, which adds an item to the end of the event queue.

The top-level scheduleMicrotask() function, which adds an item to the end of the microtask queue (if a task needs to be completed before processing any events from the event queue, you should usually execute this function first. If it cannot be executed first, add the task to the microtask queue using scheduleMicrotask().

await / async

Pictured above,

1. Look carefully at the order in which the underlined parts are executed

main… start A… start B… start C… start C… end A… end main… End ——————— AA micromission… 1 C Complete B… End B execution result then microtask… 2 ——————– BB Event queue Adds an event queue to another event queue

1. Future() adds the implementation to the practice queue

2. The code after then() and await is added to the microtask queue and is executed sequentially after the next loop.

If the function body in then() is an infinite loop, the program will be stuck. Therefore, async is not truly asynchronous. It is only a tool to control the flow of the program

5, since can block that file read and write, network request why not block? This is because IO, such as network requests and file reads and writes, can be called on a non-blocking basis;

Blocking and non-blocking calls

Blocking call: The current thread is suspended until the result of the call is returned, and the calling thread will not resume execution until it gets the result of the call.

Non-blocking calls: After the call is executed, the current thread does not stop executing. It just needs to wait for a while to check if the result is returned.

For example, the network request itself uses Socket communication, and Socket itself provides a SELECT model, which can work in a non-blocking manner.

For file read/write IO operations, we can use the event-based callback mechanism provided by the operating system.

Because the Dart is single-threaded language, when faced with delayed operations (I/O operations), sequential execution of operations in a thread will be blocked, then the app, users will feel caton, so usually use asynchronous processing to solve this problem, when need to the operation, the delay will put delay operation in the queue, does not need to delay the first operation to perform, And finally, we’ll deal with the delay. The Dart library has many functions that return Future or Stream objects. These functions are called asynchronous functions. They return after setting up some operation that takes a certain amount of time, such as an I/O operation, rather than waiting for it to complete.

You should ensure that you never block the event loop during development. In other words, each requested Dart callback should be completed quickly. These also apply to await/async, future.then

How does Dart accomplish time-consuming operations

Dart performs time-consuming operations using a single thread, event loop, and non-blocking invocation

Dio.post returns a Future object whose function will be inserted into the event queue, waiting for the next execution, not necessarily of the result, but of the query result

True asynchrony still requires multiple threads

isolate

Flutter provides a simple way to use the ISOLATE, compute