Description of synchronous and asynchronous, and js task queue.

javascript is a single-threaded language, so when performing a task, all tasks must be queued and then executed one by one. This is the synchronous mode

  So synchronization task means that queued for execution on the main thread task, only the first task is finished, you can perform the task;

  Asynchronous task refers, not to enter the main thread, and enter the task "Task Queue" (task queue), and only the main thread and other task is finished, "Task Queue" Start notify the main thread, the request to perform the task, the task will enter The main thread execution.

So js operating mechanism is as follows:

  (1): all synchronization tasks are executed on the main thread, forming an execution stack.

  (2): to the main thread, there is a "Task Queue" (task queue). Asynchronous tasks into the task queue for the main thread is finished.

  (3): Once the "execution stack" of all the synchronization task is completed, the system reads the "Task Queue", corresponding to the end of the asynchronous tasks waiting state into the execution stack, started.  

How do you know the main thread task is empty: there is the process of monitoring process JS engine, whether it will continually checks the main thread execution stack is empty, if there is a function once empty, it will go there to check Event Queue waiting to be called

Guess you like

Origin www.cnblogs.com/byyoki/p/11777579.html