In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
What is Event Loop, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Event Loop is a very important concept, which refers to an operating mechanism of a computer system.
JavaScript language uses this mechanism to solve some problems caused by single-thread operation.
This article refers to C. Aaron Cois's "Understanding The Node.js Event Loop" to explain what Event Loop is and how it relates to the single-threaded model of the JavaScript language.
If you want to understand Event Loop, you should start with the mode in which the program runs. The program after running is called "process". In general, a process can only perform one task at a time.
If there are a lot of tasks to perform, there are no more than three solutions.
(1) queue up. Because a process can only perform one task at a time, we have to wait until the previous task is finished before performing the following task.
(2) New process. Use the fork command to create a new process for each task.
(3) create a new thread. Because processes consume too much resources, today's programs often allow a process to contain multiple threads, which complete the task. For a detailed explanation of processes and threads, please see here. )
Take the JavaScript language as an example, it is a single-threaded language, all tasks are done on a single thread, that is, using the above method. Once encounter a large number of tasks or encounter a time-consuming task, the web page will appear "fake death", because the JavaScript can not stop, it will not be able to respond to the user's behavior.
You might ask, why is JavaScript single-threaded, and can't it be implemented as multithreading?
It has to do with history. JavaScript has been single-threaded since its birth. The reason is probably that you don't want browsers to get too complicated, because multithreads need to share resources and have the potential to modify each other's running results, which is too complex for a web scripting language. It was later agreed that JavaScript was a single-threaded language. Worker API can implement multithreading, but JavaScript itself is always single-threaded. )
If a task is time-consuming, for example, it involves a lot of Igamot O (input / output) operations, then the thread runs like this.
The green part of the image above is the running time of the program, and the red part is the waiting time. As you can see, most of the running time of this thread is empty waiting for the return result of the Iwhite O operation because it is very slow. This mode of operation is called "synchronous mode" (synchronous Imax O) or "blocking mode" (blocking I peg O).
If you use multithreading and run multiple tasks at the same time, this is probably the case.
The figure above shows that multithreading not only takes up many times the system resources, but also idle many times the resources, which is obviously unreasonable.
Event Loop is proposed to solve this problem. Wikipedia defines it as follows:
"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.)"
To put it simply, it is to set up two threads in the program: one is responsible for the running of the program itself, called the "main thread", and the other is responsible for the communication between the main thread and other processes (mainly various Imax O operations), which is called "Event Loop thread" (which can be translated as "message thread").
The green part of the main thread above still indicates the run time, while the orange part indicates the idle time. Whenever the main thread encounters an iThread O, the main thread asks the Event Loop thread to notify the corresponding Imax O program, and then runs back, so there is no red wait time. Wait until the Event Loop O program finishes the operation, and then return the result to the main thread. The main thread invokes the callback function set in advance to complete the whole task.
As you can see, due to the extra orange free time, the main thread is able to run more tasks, which improves efficiency. This mode of operation is called "asynchronous mode" (asynchronous I asynchronous O) or "non-blocking mode" (non-blocking mode).
This is how the JavaScript language works. Although the single-threaded model poses great restrictions on JavaScript, it also gives it an advantage that other languages do not have. If deployed well, JavaScript programs will not be blocked, which is why the node.js platform can cope with heavy traffic with few resources.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.