js-Event Loop

js-Event Loop directory


Preface

  • Event LoopIs a very important concept

Recommended reading

what isEvent Loop

  • Event Loop is a very important concept, referring to an operating mechanism of a computer system.

  • The JavaScript language uses this mechanism to solve some of the problems caused by single-threaded operation.

img

This article refers to "Understanding The Node.js Event Loop" by C. Aaron Cois to explain what an Event Loop is and how it relates to the single-threaded model of the JavaScript language.

To understand Event Loop, we must start with the operating mode of the program. The program after running is called "process" (process). Generally, a process can only execute one task at a time.

Multitasking method

If there are many tasks that need to be performed, there are no more than three solutions.

  1. **queue. **Because a process can only perform one task at a time, you have to wait for the previous task to finish before executing the following task.
  2. **New process. **Use the fork command to create a new process for each task.
  3. **Create a new thread. **Because the process is too resource-intensive, today's programs often allow a process to contain multiple threads, and the threads are used to complete the task. (For a detailed explanation of processes and threads, please see here .)

JSSingle-threaded problem

Take JavaScript language as an example, it is a single-threaded language, all tasks are completed on one thread, that is, the first method above is adopted. Once encountered a large number of tasks or encountered a time-consuming task, the web page will appear "fake death", because JavaScript can not stop, it can not respond to the user's behavior.

You may ask, why is JavaScript single-threaded and can't it be implemented as multi-threaded?

This has something to do with history. JavaScript has been single-threaded since its inception. The reason is probably that you don't want to make the browser too complicated, because multiple threads need to share resources and may modify each other's running results, which is too complicated for a web scripting language. Later, it was agreed that JavaScript is a single-threaded language. (Worker API can achieve multi-threading, but JavaScript itself is always single-threaded.)

If a task is time-consuming, such as involving a lot of I/O (input/output) operations, the running of the thread is probably as follows.

img

The green part of the above figure is the running time of the program, and the red part is the waiting time. As you can see, because the I/O operation is very slow, most of the running time of this thread is waiting for the return result of the I/O operation. This mode of operation is called "synchronous I/O" or "blocking I/O".

If you use multiple threads to run multiple tasks at the same time, it is likely to be like this.

img

The above figure shows that multi-threading not only takes up multiple times of system resources, but also multiple times of idle resources, which is obviously unreasonable.

Event Loop

  • The core is: callback function, asynchronous mode

Event Loop is proposed to solve this problem. Wikipedia defines it like this:

" Event Loop is a program structure for waiting and sending messages and events. (a programming construct that waits for and dispatches events or messages in a program.)"

Simply put, two threads are set up in the program: one is responsible for the operation of the program itself, called the "main thread"; the other is responsible for the communication between the main thread and other processes (mainly various I/O operations), called "Event Loop thread" (can be translated as "message thread").

img

The green part of the main thread in the above figure still represents the running time, while the orange part represents the idle time. Whenever I/O is encountered, the main thread will let the Event Loop thread to notify the corresponding I/O program, and then continue to run, so there is no red waiting time. After the I/O program completes the operation, the Event Loop thread returns the result to the main thread. The main thread calls the preset callback function to complete the entire task.

It can be seen that due to the extra orange idle time, the main thread can run more tasks, which improves efficiency. This operation mode is called " asynchronous " (asynchronous I / O) or "non-blocking mode" (non-blocking mode).

to sum up

This is exactly how the JavaScript language works. Although the single-threaded model poses a great limitation to JavaScript, it also gives it advantages that other languages ​​do not have. If deployed well, JavaScript programs will not be blocked, which is why the node.js platform can use very few resources to cope with large traffic visits.

Guess you like

Origin blog.csdn.net/u013362192/article/details/115156383