nodejs is not multi-threaded, but single-threaded; nodejs uses a single-threaded asynchronous non-blocking mode. Because of the JavaScript engine, node is single-threaded by default. A nodejs application cannot utilize multi-core resources and can use event-driven and asynchronous "I/O" method to achieve a single-threaded, high-concurrency runtime environment.
The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.
Node is single-threaded and uses single-threaded asynchronous non-blocking mode.
Because of the JavaScript engine, node is single-threaded by default, and a node.js application cannot utilize multi-core resources.
Node.js uses event-driven and asynchronous I/O to implement a single-threaded, high-concurrency runtime environment. Single-threading means that only one thing can be done at the same time.
Nodejs implements asynchronous and non-blocking:
In nodejs, only js execution is actually single-threaded, and I/O is obviously other threads.
The js execution thread is single-threaded. As long as the required I/O is passed to libuv, it will come back to do other things immediately, and then libuv will call back at the specified time.
In detail, nodejs first calls it from the js code in node-bindings to the C/C code, and then encapsulates something called the "request object" in the C/C code and passes it to libuv. Some of this request object is similar to function callbacks that need to be executed. Just execute the callback to libuv and implement the callback after execution.
In short, the general process of asynchronous I/O is as follows.
1. Start an I/O call
The user calls the Node core module from Javascript code and passes parameters and callback functions to the core module
The Node core module encapsulates the passed parameters and callback functions in the request object.
Push this request object to the I/O thread pool to wait for running;
The Javascript asynchronous call ends, and the Javascript thread continues to perform subsequent operations.
2. Execute callback
After the I/O operation is completed, the callback function previously encapsulated in the request object is retrieved and executed to achieve the purpose of the Javascript callback. (The details of the callback here are explained below)
It can be seen from here that we have actually always misunderstood the single thread of Node.js.
In fact, single thread refers to a single thread in the Javascript runtime environment, and Node.js does not have the ability to create new threads when running Javascript. The final actual operations are still performed in Libuv and its event loop. This is why Javascript, a single-threaded language, can implement asynchronous operations in Node.js. The two will not conflict.
Expand your knowledge:
Node.js Multithreading Overview
Some people may say that although Node.js is single-threaded, it can use cyclic events (Event Loop) to implement concurrent execution tasks. Investigating its essence, NodeJs actually uses two different threads, one is the main thread for processing loop events, and the other is some auxiliary threads in the Worker pool. The main functions and relationships of these two threads are as follows:
Recommended learning: "nodejs video tutorial"
The above are the details of whether nodejs is multi-threaded. For more information, please pay attention to other related articles on this site!