Single thread, synchronous asynchronous, blocking non-blocking understanding

Source: Internet
Author: User
Tags message queue square root

I. Conceptual understanding

1. Synchronous Asynchronous:

Synchronous and asynchronous concern is that the message communication mechanism (synchronous communication/asynchronous communication) is called synchronization, that is, when a * call is emitted, the * call does not return until the result is obtained. But once the call returns, it gets the return value. In other words, it is the result of the * caller actively waiting for this * call *.

Instead of async, the call is returned immediately after it is emitted, so no results are returned. In other words, when an asynchronous procedure call is made, the caller does not get the result immediately. Instead, the caller is notified by status, notification, or by a callback function to handle the call after the * call * is issued.

2. Blocking non-blocking:

Blocking and non-blocking concerns the state of the program when it waits for the call result (message, return value).

A blocking call means that the current thread is suspended until the call results are returned. The calling thread will not return until the result is obtained.
A non-blocking call means that the call does not block the current thread until the result is not immediately available.

Serious
Links: https://www.zhihu.com/question/19732473/answer/20851256
Source: Know

3. Single Thread:

We often say that "javascript is single-threaded".

The so-called single-threaded, refers to the JS engine is responsible for interpreting and executing JavaScript code of the thread only one. You might call it the main thread.

But there are actually other threads. For example: threads that handle AJAX requests, threads that handle DOM events, timer threads, threads that read and write files (for example, in node. js), and so on. These threads may exist within the JS engine or may exist outside of the JS engine, where we do not differentiate. You might call them worker threads.

Two. JS Synchronous Asynchronous Understanding:

1. Sync:

MATH.SQRT (2); Console.log (' Hi '); When the first function returns, it gets the expected return value: The square root of 2. When the second function returns, you see the expected effect: A string is printed in the console

2 Async: (Take async function settimeout as an example)

SetTimeout (function() {Console.log ("a")}, andConsole.log ("B") ; // "B" // "a"
When the SetTimeout function returns, it does not immediately get the result, but runs the later program until the function notification is called (notification after 500ms).

3. Elements of an asynchronous process:

The main thread initiates an asynchronous request, and the corresponding worker thread receives the request and informs the main thread that it has received it (the asynchronous function returns); The main thread can continue executing the following code while the worker thread executes the asynchronous task, the main thread is notified after the worker has finished the work, and the main thread receives the notification and executes a certain action (call the callback

So, from the main thread point of view, an asynchronous process consists of the following two elements:

    • Initiating a function (or called a registration function)

    • callback functioncallbackFn 

4. Message Queuing and Event loops:
The worker thread puts the message in the message queue, and the main thread takes the message through the event loop process. The message is the callback function that was added when the asynchronous task was registered.

    • Message Queuing: Message Queuing is a first-in-one-out queue that contains various messages.

    • Event Loop: The event loop is the process by which the main thread repeatedly takes messages from the message queue and executes them.

Author: Manxisuo

Links: 1190000004322358

Three. JS in several ways of asynchronous programming

1. Callback function:

Suppose there is a function

F1 (); F2 ();
F2 must run after F1 is complete, and F1 is a time-consuming application

You can use asynchronous calls:

function F1 (F2) {
SetTimeout (function () {
Console.log ("a")
F1 's Task code
F2 ()}, 1000)}
function F2 () {Console.log ("B")};
F1 (F2);
Console.log ("C"); }

C

A

"B"

In this way, the synchronous operation becomes an asynchronous operation;

2. Event monitoring: Event-driven mode is used. The execution of a task does not depend on the order of the Code, but on whether an event occurs.

take F1 and F2 as an example.  First, bind an event for F1 (the jquery notation used here). F1.on (' Done ', F2); This line of code means that F2 is executed when the Done event occurs F1. Then, rewrite the F1:  function  F1 () {setTimeout() (function  () {  //  F1 any  Service Code F1.trigger (' Done '); }f1.trigger (' done ') indicates that when execution is complete, the Do event is immediately triggered to begin execution of F2.

3. Publish, Subscribe:

4.Promise objects:

The promises object is a specification proposed by the COMMONJS workgroup to provide a unified interface for asynchronous programming. Simply put, the idea is that each asynchronous task returns an promise object that has a then method that allows you to specify a callback function. For example, F1 's callback function, F2, can be written as:

Single thread, synchronous asynchronous, blocking non-blocking understanding

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.