Back to the blog talk about asynchronous and single-threaded in node

Source: Internet
Author: User

Back to the blog, this account before the registration is only a blog post. Listen to a friend's suggestion and decide to update two times a week.

The first one discusses some of the more asynchronous problems you've been thinking about lately.

Traditional multithreaded asynchronous

Traditional asynchronous is multi-threaded, when to do two things at the same time, they are executed in different lines thread. It's like selling things at the counter, and a person has to find an employee to accompany him until the person is gone and the employee can receive the next guest. The staff in the shop is like the thread constructor idle threads, can go to receive the guest when the idle time, but at the same time only receives one person, must receive other person to need to find another person.

The thread in the computer is the equivalent of an employee team and where to go. The asynchronous advantage of multithreading is that it can take up the resources of the system more, each time to open up a thread, like to find a person from this team, the team is so many people, rob one over a little more labor, reception more guests. The disadvantage of this is that the cost of creating and destroying threads is very large, and each thread needs to use resources so that the resources are not allocated and the counter is not able to accommodate too many guests. And if there is a guest who has been watching for too long, a receptionist will not be able to receive other guests, so the resources that are already strained will not be allocated.

Node. JS Single-Thread Async

If you're stuck with traditional asynchrony, you'll surely question: how can a single thread be asynchronous?

Let's think of a question, what is async? The most straightforward answer is "get two operations in parallel". But in a single thread, only one thing can be done at a time, how can there be async. If it is normal operation is not possible, but IO can, because the wait time IO operation is not to occupy any system resources, that is to say you can put it slowly, as long as the implementation of the notification I will be done.

What do you mean, we take the example of boiling water and cooking.

In the mode of synchronization, we first boil water, we will wait for the boil, boil and then to cut vegetables, cook rice, and then after the meal cooked and then to stir fry.

In the multi-threaded asynchronous, we first boil water, this at the same time we want to cut vegetables and cooking, it is called another person to cut vegetables, after cutting vegetables he found to cook, but his next to stir-fry, then see just boil the people burned no, burn finished let him cook rice, did not burn and then call another person to cook rice, their own Finally, we all finished, this thing even play.

and a single-threaded asynchronous, we boil water, and then put it in there to burn, and then to cut vegetables. At this time the vegetables cut out, first to check the water to burn well did not, if the burn is good with this water to do something, if not burned, continue to burn, and then cook rice and boil water, I do not put people to stare at whether to cook, but to finish a thing to see whether it is done well.

In node, the use of notifications for IO operations, rather than traditional asynchronous operations, uses threads to monitor him.

My understanding of node's event polling is that the main thread assigns the task down and registers the callback function. Main Line thread in the execution of the code when IO continues to wait, the main thread after the completion of the polling, because this time the main thread is idle state, so you can always poll until you find that an IO operation sent him a signal to tell him I'm done now, you can use this data, the main thread of the resources to carry out the callback function. That is, from the beginning to the end, only the main thread is doing things, the main thread is either executing the function, or in the event of polling, to find out which event has completed the need to execute his callback function.

In other words, we continue to use just the example, this person is not cutting vegetables or stir-frying, is to check whether the water is boiling, the rice is not cooked. So even a single thread can still use asynchronous mode.

Summarize

Multithreading is not a necessary factor for asynchrony, here we talk about the Io,io operation itself is not executed in the program, but to others to do, and so he finished we get the results. What I think about most before I am IO is not blocking, and then understand the IO when the thread is 0 consumed, completely in the waiting state.

The so-called single-threaded asynchronous, it is reasonable to allocate the IO waiting time, let the main thread to do other things, in the main thread idle time only to check the waiting task has not been completed. In general, node is suitable for IO, high concurrency, because these things do not have to wait, and do not have to create new threads, but to others to do, node only need to wait for the results of the good. But it is not suitable for computationally intensive things, because when one computation blocks the main thread, it is impossible to get the next task polled, and all subsequent tasks will be blocked.

Back to the blog talk about asynchronous and single-threaded in node

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.