Internet is coming, internet is coming

Source: Internet
Author: User

Internet is coming, internet is coming
1. What is "Asynchronous non-blocking "?

This name sounds ugly, but ifBuy underpantsIn this case, it is easy to understand the meaning of "Asynchronous non-blocking.

For example, if you are a CPU thread, You need to executeProcedures for buying underpantsThe steps you need to perform are roughly as follows,

At this time, as a thread, you may encounter several situations or choices.

Obviously, the third method is the smartest, and the first two methods are slightly less efficient. So according to our understanding,

Synchronization or non-synchronization indicates whether the entire process needs to be completed in sequence

Blocking or non-blocking means that the function you call will notInstantTell you the result

Ii. Blocking and Asynchronization in js

You have a function and a program.

2.1 synchronization blocking in js
// This is a blocking function that copies one file to another. function copyBigFile (afile, bfile) {var result = copyFileSync (afile, bfile); return result ;}

Call the "copyBigFile ()" function to copy a large file to another file, which takes one hour. It means that the function will be returned in an hour.

// This is a program console. log ("start copying... "); var a = copyBigFile('A.txt ',' B .txt '); // This line of program will take 1 hour to console. log ("Finished"); // This line of program will run the console in an hour. log ("handle other things"); // This line of program will execute the console in an hour. log ("Hello World, the whole program has been loaded, please enjoy"); // This line of program will be executed in an hour

The above program is an example of synchronization blocking. because the process of returning the value of the copyFileSync function takes a long time, the thread cannot continue to execute and can only wait.

2.2 Non-blocking synchronization in js
// This is a non-blocking function // true is returned if the replication is completed. If not, falsefunction copyBigFile (afile, bfile) {var copying = copyFileAsync (afile, bfile); var isFinished =! Copying; return! IsFinished ;}

Calling this function will immediately return the result, and then your program can be written

Console. log ("start copying... "); while (a = copyBigFile('A.txt ',' B .txt ') {console. log ("other tasks can be handled here");}; console. log ("Finished"); // This line of program will run the console in an hour. log ("Hello World, the whole program has been loaded, please enjoy"); // This line of program will be executed in an hour

A non-blocking function brings more convenience to your programming. You can write other programs while performing long IO operations to improve efficiency. The execution result is as follows:

Start copying... in this case, you can handle other things. In this case, you can handle other things. In this case, you can handle other things... finishedHello World. The whole program has been loaded. Please use
2.3 asynchronous non-blocking in js

We can see that a non-blocking function can bring us a lot of flexibility in programming, and we like non-blocking functions.
However, we can see that the synchronization program needs to poll the result in a loop.The program will be executed multiple times., So it is difficult to control to write some normal programs, and it is difficult to reuse them.
Therefore, we need a more reasonable way to use non-blocking functions.
That is, I will not take the initiative to ask the results, but will notify me when you have the results.
// This is a non-blocking function
// If the copy is completed, true is returned. If the copy is not completed, false is returned.

// Non-blocking functions with asynchronous notification capabilities // you do not need to understand the following. You only need to know that this function will execute successfunction copyBigFile (afile, bfile, callback) after completing the copy operation) {var copying = copyFileAsync (afile, bfile, function () {callback () ;}); var isFinished =! Copying; return! IsFinished ;}

This function is different from the previous non-blocking synchronous function in that it hasNotification FunctionIn other words, it can actively notify the program after completing the operation, "I have finished ". So there is a program as follows,

Console. log ("start copying... "); copyBigFile (" A.txt "," B .txt ", function () {console. log ("Finished"); // The console is executed one hour later. log ("Hello World, the entire program has been loaded, please enjoy"); // executed in an hour}) console. log ("do other things"); console. log ("Do some other processing ");

After the program calls the copyBigFile function, it can immediately obtain the returned value. The thread is not blocked, so it can do other things. Then, after the copyBigFile is completed, it will execute the specified function. So the program output should be,

Start copying... do other things and do some other work. FinishedHello World. The whole program has been loaded. Please enjoy

In this case, the program is easier to control and the process is clearer. Some "other things" can be processed before the function is notified, which fully improves the utilization efficiency of the thread.

Iii. Improvement

In the above program, our program needs to copy a huge file. In fact, the process of copying files is left to the system's IO calls, and our threads do not need to process the copy details.
So passNon-blockingFunction, we canPossibleUse thread resources to do other things.
And passAsynchronous callTo make the program flowEasier ControlTo more efficiently use thread resources.

Js implements Asynchronization by passing functions. (You can also use promise to implement Asynchronization ...)

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.