Is nodejs multi-threaded?

WBOY
Release: 2022-06-29 10:27:46
Original
3735 people have browsed it

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. Able to use event-driven and asynchronous "I/O" methods to achieve a single-threaded, high-concurrency runtime environment.

Is nodejs multi-threaded?

The operating environment of this article: Windows 10 system, nodejs version 12.19.0, Dell G3 computer.

Is nodejs multi-threaded?

Node is single-threaded and adopts 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, and single-threading means that only one thing can be done at the same time.

nodejs implements asynchronous, non-blocking:

nodejs actually only has js execution that is 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 is first called from the js code in node-bindings to the C/C code, and then encapsulates the content 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 I/O call

The user calls the Node core module from Javascript code and passes the parameters and callback function to the core module

The Node core module will pass The parameters and callback functions are encapsulated 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 will be retrieved and executed to achieve the purpose of 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. 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.

Extended knowledge:

Node.js multi-threading overview

Some people may say that although Node.js is single-threaded, it can be used Loop events (Event Loop)l to achieve concurrent execution of 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 shown in the figure

Is nodejs multi-threaded?

Recommended learning: "nodejs video tutorial"

The above is the detailed content of Is nodejs multi-threaded?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!