Table of Contents
Is nodejs multi-threaded?
Home Web Front-end Front-end Q&A Is nodejs multi-threaded?

Is nodejs multi-threaded?

Jun 29, 2022 am 10:27 AM
nodejs

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!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The difference between nodejs and tomcat The difference between nodejs and tomcat Apr 21, 2024 am 04:16 AM

The main differences between Node.js and Tomcat are: Runtime: Node.js is based on JavaScript runtime, while Tomcat is a Java Servlet container. I/O model: Node.js uses an asynchronous non-blocking model, while Tomcat is synchronous blocking. Concurrency handling: Node.js handles concurrency through an event loop, while Tomcat uses a thread pool. Application scenarios: Node.js is suitable for real-time, data-intensive and high-concurrency applications, and Tomcat is suitable for traditional Java web applications.

The difference between nodejs and vuejs The difference between nodejs and vuejs Apr 21, 2024 am 04:17 AM

Node.js is a server-side JavaScript runtime, while Vue.js is a client-side JavaScript framework for creating interactive user interfaces. Node.js is used for server-side development, such as back-end service API development and data processing, while Vue.js is used for client-side development, such as single-page applications and responsive user interfaces.

Is nodejs a backend framework? Is nodejs a backend framework? Apr 21, 2024 am 05:09 AM

Node.js can be used as a backend framework as it offers features such as high performance, scalability, cross-platform support, rich ecosystem, and ease of development.

What is the difference between npm and npm.cmd files in the nodejs installation directory? What is the difference between npm and npm.cmd files in the nodejs installation directory? Apr 21, 2024 am 05:18 AM

There are two npm-related files in the Node.js installation directory: npm and npm.cmd. The differences are as follows: different extensions: npm is an executable file, and npm.cmd is a command window shortcut. Windows users: npm.cmd can be used from the command prompt, npm can only be run from the command line. Compatibility: npm.cmd is specific to Windows systems, npm is available cross-platform. Usage recommendations: Windows users use npm.cmd, other operating systems use npm.

Is nodejs a back-end development language? Is nodejs a back-end development language? Apr 21, 2024 am 05:09 AM

Yes, Node.js is a backend development language. It is used for back-end development, including handling server-side business logic, managing database connections, and providing APIs.

What are the global variables in nodejs What are the global variables in nodejs Apr 21, 2024 am 04:54 AM

The following global variables exist in Node.js: Global object: global Core module: process, console, require Runtime environment variables: __dirname, __filename, __line, __column Constants: undefined, null, NaN, Infinity, -Infinity

Is there a big difference between nodejs and java? Is there a big difference between nodejs and java? Apr 21, 2024 am 06:12 AM

The main differences between Node.js and Java are design and features: Event-driven vs. thread-driven: Node.js is event-driven and Java is thread-driven. Single-threaded vs. multi-threaded: Node.js uses a single-threaded event loop, and Java uses a multi-threaded architecture. Runtime environment: Node.js runs on the V8 JavaScript engine, while Java runs on the JVM. Syntax: Node.js uses JavaScript syntax, while Java uses Java syntax. Purpose: Node.js is suitable for I/O-intensive tasks, while Java is suitable for large enterprise applications.

How to deploy nodejs project to server How to deploy nodejs project to server Apr 21, 2024 am 04:40 AM

Server deployment steps for a Node.js project: Prepare the deployment environment: obtain server access, install Node.js, set up a Git repository. Build the application: Use npm run build to generate deployable code and dependencies. Upload code to the server: via Git or File Transfer Protocol. Install dependencies: SSH into the server and use npm install to install application dependencies. Start the application: Use a command such as node index.js to start the application, or use a process manager such as pm2. Configure a reverse proxy (optional): Use a reverse proxy such as Nginx or Apache to route traffic to your application

See all articles