It's hard to understand the single thread of Javascript

Source: Internet
Author: User
Tags sigint signal

It's hard to understand the single thread of Javascript.

For example, writing a while (true) for C-system programming is not a very stressful thing. There doesn't seem to be any surprises in Nodejs, because there's a message mechanism to manipulate--it's just your illusion of nodejs, or that's just my illusion of nodejs. If the sub-process is written like this:

Process.on (' message ', function () {while  (true);}); Process.on (' SIGINT ', function () {  console.log (' Child Process killed by SIGINT Signal. ');  Process.exit ();}); Process.on (' SIGTERM ', function () {  console.log (' Child Process killed by SIGTERM Signal. ');  Process.exit ();});

What happens when the main process sends a Kill? On my MAC, the child process gets out of control, the CPU runs full, and the fan goes crazy.

Because in Nodejs, a while (true) allows EventLoop to die directly.

For Javascript, the setinterval is applied. Remember before is someone reminds me, will use setinterval, climb out of the pit to understand deeply, is the will use.

So, in Javascript, you should disable while doing round robin or waiting.

--------------------------------

In Nodejs, the concept of "threading" is implemented in so many ways, Process.nexttick, SetTimeout, Setimmediate, SetInterval, and asynchronous programming. But they are not really "threads", such as do not sleep (), do not have accurate timing, in fact, is still a thread, the process is just such a thread. If there is a need to pinch time to schedule the work, or more heavy-duty time-consuming work, still have to open the sub-process is similar.

--------------------------------

There seems to be no such concept of "critical area" in Nodejs and the problems it brings. Because you don't allow inter-process sharing of resources at all. Public data can only be written in third-party services. Static configuration Fortunately, written in a module is finished. Dynamic configuration and dynamic information ... The simplest is ... Read and Write files ... It's not a problem if it doesn't happen frequently. Frequently, you have to be on heavy equipment, such as Redis ...

--------------------------------

With Javascript, it's time to get a sense of how this language should be thought, or language, and its running strategy in Turing. When I interview someone asking for a language, the person who really uses it, says it, not what it does, but what it has and can't. The experience is the one who knows it.

It's hard to understand the single thread of Javascript

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.