Some understanding of JavaScript threads and processes
in Note with 1 comment
Some understanding of JavaScript threads and processes
in Note with 1 comment

This article mainly discusses the problems of JavaScript in single thread and its solutions. Focusing on the browser and Node.js, the content may not be precise and there may be errors. Please point out···

Web Workers

As we all know, JavaScript runs in a single thread mode in browsers.

The biggest advantage of single thread is that the program has a single state, so you don't have to pay attention to the state synchronization problem like multithreaded programming. There is no deadlock, and there is no performance overhead caused by thread context exchange. Of course, the weakness is also obvious. For example, the multi-core CPU cannot be used. Errors cause the entire application to quit and the CPU is occupied by computing, which makes it impossible to continue working.

For the problem of large amount of computing under single thread, WebWorkers in the HTML5 standard formulated by the W3C is to solve this problem. This allows JavaScript to create multiple threads, which are controlled by the main thread, deliver the calculation work to the child thread, and then pass the results through message passing.

For example:

 var worker = new Worker('worker.js'); //Receive the data from the worker worker.onmessage = function(event){ console.log(event.data); };
 // woker.js var i = 0; function timedCount(){ for(var j = 0, sum = 0; j < 100; j++){ for(var i = 0; i < 100000000; i++){ sum+=i; }; }; //Send the obtained sum back to the main thread postMessage(sum); }; //Send the time before executing timedCount back to the main thread through postMessage postMessage('Before computing, '+new Date()); timedCount(); //After ending timedCount, send the end time back to the main thread postMessage('After computing, ' +new Date());

Node.js process

On a single thread, JavaScript will cause the entire process to crash once an exception occurs in the single thread and is not captured. Node.js uses the same idea as WebWorkers to solve the problem of large amount of computation in a single thread, that is, sub processes. By creating sub processes, we can make full use of multi-core CPUs and solve the problem of single thread robustness. By assigning the calculation task to each sub process, a large number of calculations are decomposed, and then the results are transmitted through the event messages between processes.

For example:

 // master.js var fork = require('child_process').fork; var cpuCount = require('os').cpus().length; for(var i = 0; i < cpuCount; i++){ fork('./worker.js'); }
 // worker.js var http = require('http'); http.createServer(function(req,res){ res.writeHead(200,{'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(Math.round((1 + Math.random()) * 1000),'127.0.0.1');

adopt node master.js Start, in the * nix system, enter ps aux|grep worker.js Command line to view the number of processes, as shown below

 Chakhsu 44764 0.0 0.3 3033512 25496 s000 S+11:12 PM 0:00.12/usr/local/bin/node/ worker.js Chakhsu 44763 0.0 0.3 3033512 25436 s000 S+11:12 PM 0:00.12/usr/local/bin/node/ worker.js Chakhsu 44762 0.0 0.3 3024296 25192 s000 S+11:12 0:00.11 PM/usr/local/bin/node/ worker.js Chakhsu 44761 0.0 0.3 3033512 25564 s000 S+11:12 PM 0:00.12/usr/local/bin/node/ worker.js

This is the Master Worker model, also known as the master-slave mode. In this mode, processes are divided into two types, the main process and the working process. The main process is not responsible for specific business processing, but is responsible for scheduling or managing the working process, which tends to be stable; The work process is responsible for the specific business. Because of the particularity and diversity of the business, developers need to pay attention to the stability of the work process.

here fork() The copied processes are independent processes. Each process has an independent and brand new V8 instance. The fork behavior is expensive. Here, multiple processes are started just to make full use of the CPU. Concurrency problems are solved on a single thread through an event driven approach.

Not much content, about that

Responses
  1. dmz

    Hello, I recently used the theme of your website, and I like it very much, especially the light, small and fast. I want to add a webmaster statistics of cnzz, but no matter where I add and save it in the footer, clicking any article or webpage of the blog will jump to webmaster statistics, and I don't know how to solve it.. So here I hope your station can help me adjust the stationmaster's statistics. Email: 3416168862@qq.com

    Reply