In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of "whether node.js is single-threaded". The editor shows you the operation process through the actual case. The operation method is simple, fast and practical. I hope this "node.js is not single-threaded" article can help you solve the problem.
Node.js is single-threaded by default. Because of the JavaScript engine, node.js uses a single-thread asynchronous non-blocking mode, and a nodejs cannot make use of multi-core resources. However, a third-party library can provide multi-threading support, but the main thread is single-threaded and the operations in the main thread are blocked synchronously.
Operating environment of this tutorial: windows10 system, nodejs version 12.19.0, Dell G3 computer.
Is node.js single threaded?
Node.js is single-threaded by default, and node.js uses single-thread asynchronous non-blocking mode.
Node.js adopts single-thread asynchronous non-blocking mode. Its single thread refers to the single thread of its own Javascript running environment. Node.js does not give Javascript the ability to create new threads when it executes. It is asynchronous through Libuv and its event loop.
Node is just that the main thread is single-threaded, and the operations in the main thread are synchronously blocked. However, it throws all the operations that need to be asynchronous to the IO thread pool, and the main thread interacts with the IO thread pool to get asynchronous data through a certain event polling mechanism.
The performance of node.js is not the highest, because of the javascript engine, node.js is single-threaded by default, and a node.js application cannot utilize multi-core resources.
However, there are third-party libraries that provide multithreading support, but not seamlessly. Node.js is to solve the bottleneck (synchronous blocking calls waste threads compared to traditional techniques), it does not improve the speed of Iscaro, but resource scheduling is more efficient. If the speed of node.js O is not solved, node.js can only say that it can handle many request at the same time, but the response time of each request is still that long, or even longer.
Node is single-threaded, so how does it handle highly concurrent requests?
Rely on event-driven.
Each Node.js process has only one main thread executing program code, forming an execution stack (execution context stack).
In addition to the main thread, an event queue (Event queue) is maintained. When a user's network request or other asynchronous operation arrives, node will put it into the Event Queue. It will not be executed immediately, the code will not be blocked, and continue until the main thread code is finished.
After the execution of the main thread code is completed, then through the Event Loop, that is, the event loop mechanism, we start to fetch the first event at the beginning of the Event Queue, assign a thread from the thread pool to execute the event, then continue to fetch the second event, assign a thread from the thread pool to execute, and then the third and the fourth. The main thread keeps checking whether there are any unexecuted events in the event queue until all the events in the event queue have been executed, and then whenever new events are added to the event queue, the main thread will notify the main thread to take them out sequentially and submit them to EventLoop for processing. When an event is completed, the main thread is notified, the main thread executes the callback, and the thread is returned to the thread pool.
The main thread keeps repeating the third step above.
NodeJs single thread is only a main thread, and the essence of asynchronous operation is completed by thread pool. Node transfers all blocking operations to internal thread pool, and is only responsible for round-trip scheduling, and does not carry out real Icano operation, thus realizing asynchronous non-blocking IAsynchronous O, which is the essence of node single-threading and event-driven.
This is the end of the content about whether node.js is single-threaded or not. Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.
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.