Asynchronous operation

Single-threaded model

         It refers to a single-threaded model, JavaScript only run on a thread. JavaScript can only perform one task, other tasks must wait in line behind. JavaScript runs on a thread, not the JavaScript engine that only one thread. JavaScript engine has multiple threads can only run a single script (called the main thread) on a thread, other threads are fitted in the background. The reason why the use of single-threaded, not multi-threaded, JavaScript is single-threaded since birth, because the browser did not want to become too complicated because multiple threads need to share resources, and it is possible to modify the operating results of each other, for a web page scripting language, it would be too complicated.

Benefits: It is relatively simple to implement, execution environment is relatively simple;

Disadvantages: As long as there is a very long time-consuming task, the task must be behind waiting in line, it will delay the implementation of the entire program.

 

Synchronous and asynchronous tasks Tasks

Program which all tasks can be divided into two categories: synchronization tasks (synchronous) and asynchronous tasks (asynchronous).

Synchronization tasks are those that have not been suspended engine line up on the main thread of execution tasks. Only the first task is finished before after performing a task.

Asynchronous task those who have been the engine aside, do not enter the main thread, into the mission of the task queue. Only engines consider an asynchronous tasks can be performed (such as Ajax operation results are obtained from the server), the task (in the form of callback function) will enter the main thread. At the back of asynchronous task code, without waiting for the end of the asynchronous task will run immediately, that is, the asynchronous task does not have to "plug" effect.

Event loop and task queue

JavaScript runtime, in addition to the main thread of a running engine also provides a job queue (task queue), which is asynchronous tasks need to deal with the current program. First, the main thread to perform all the synchronization tasks. Wait until all the synchronization tasks executed, it will go to the task queue inside asynchronous tasks. If the condition is satisfied, then the asynchronous task to re-enter the main thread begins execution, then it becomes a synchronization task. Wait until executed, the next asynchronous task and then enter the main thread begins execution. Once the task queue is empty, program execution ends.

Writing asynchronous tasks usually callback function. Once the asynchronous task to re-enter the main thread, executes the corresponding callback function. If the task is not an asynchronous callback, will not enter the task queue, that is, will not re-enter the main thread, because there is no next operation is specified using a callback function.

How asynchronous JavaScript engine knows there is no task results, the main thread can not enter it? The answer is that the engine is constantly checking over and over again, as long as the synchronization task execution is over, check the engine will go to those who hang up asynchronous task is not to enter the main thread. This cycle inspection mechanism, called event loop (Event Loop)

 

Asynchronous mode operation

Callback

Asynchronous operation callback function is the most basic method.

For example, two functions f1 and f2, f2 programming intention is f1 must wait until execution is complete before execution.

Event Listeners

Another idea is to use the event-driven model. Perform asynchronous tasks does not depend on the order code, but on whether an event occurred.

Or in f1 and f2, for example. First, to bind an event f1

f1.on('done', f2);

Above this line of code means that, when done f1 incident occurred on the implementation of f2. Then, f1 rewrite:

Publish / Subscribe

Events can be understood as a "signal", if there is a "signal center", a task execution is completed, the "release" (publish) a signal to the signal center, other tasks can "subscribe" (subscribe) the signal to the signal center so that you can know when to begin. This is called " publish / subscribe model " (publish-subscribe pattern), also known as " observer mode " (observer pattern).

Controlling the operation of the asynchronous process

If there are multiple asynchronous operations, there is a flow control problem: How to determine the order of execution of asynchronous operation, and how to ensure compliance with this order.

Serial execution

We can write a process control function, it is controlled asynchronous task, after a task is completed, then perform another. This is called serial execution.

Parallel execution

Flow control function can be executed in parallel, that is, all asynchronous tasks performed simultaneously, until later completed before final execution function.

Parallel and serial combined

The so-called combination of parallel and serial, is to set a threshold, a maximum of only n parallel execution of asynchronous tasks, thus avoiding excessive system resources.

 

Guess you like

Origin www.cnblogs.com/hjy-21/p/12324568.html