Theoretical Discussion: nodejs’s Single Thread Problem

  node.js, question

Long ago there was only one thread.
Later developed into multi-threading
Now single thread is popular again, nodejs

What is the difference between the current single-threaded language and the earliest single-threaded
Also, is multithreading still popular and will it be replaced by single thread?

  1. The earliest single threads were all synchronized code, which was very inefficient, and the program had been blocking and waiting for synchronization results. butNowSaid the single thread, for examplenodejsHowever, it is asynchronous and will not wait for the result of the call. Instead, it will perform other things. When the result of the call returns, it will continue to execute the program after obtaining the result.

  2. Multithreading is of course popular. The efficiency of multithreaded programs is much higher when the concurrency is high than that of single threads. There are also advantages in task segmentation. Of course, in order to make full use of machine resources, single thread has also developed idea that utilizes Central Processor multi-core. For examplenodejsTheclusterModules. butclusterAlthough the module uses multiple cores to generate the number of processes corresponding to the stone number, each process is independent of each other and shares a port. However, if it is a multi-process program such as java, the process has shared resources, thus occupying less memory.

  3. Each has its own advantages and will only make up for them and move forward. There may be a new model to replace it! Personal opinion. I hope it will help you! !

  • AttachedclusterAn example of

const cluster = require('cluster');
 const http = require('http');
 const numCPUs = require('os').cpus().length;
 if (cluster.isMaster) {
 // Fork workers.
 for (var i = 0;   i < numCPUs;  I plus)
 cluster.on('exit', (worker, code, signal) => {
 console.log(`worker ${} died`);
 } else {
 // Workers can share any TCP connection
 // In this case it is an HTTP server
 http.createServer((req, res) => {
 res.end('hello world\n');

The program will start the node process corresponding to the number of stone in Central Processor and listen on port 8000