js single-threaded asynchronous

Threads and processes:

Process is a system resource allocation and scheduling unit. A running program corresponds to a process. In windows, each running application to open or background applications, such as running qq, Google browser, Netease cloud music, Explorer, etc. are all a process. A process including the procedures and processes used to run in the memory and system resources. For example, while listening to music, while writing a blog blog garden, which is open two processes.

Thread is the executor under the process, a process will be open at least one thread (the main thread), you can also open multiple threads. Such as NetEase cloud again play music, display lyrics again, Netease cloud is the process, play music and display lyrics are two threads in the process Netease cloud.

Synchronous and asynchronous:

Synchronous Asynchronous refers to the act of the program. Synchronization program the calling time, wait until results are returned. It will not return before to no avail. In other words, the synchronization is waiting caller active procedure calls.

After issuing the asynchronous call is immediately returned, but will not return results immediately. The caller does not have to wait for the initiative, when the caller will get the results of the initiative to inform the caller.

The above concept may be more official and difficult to understand. Popular, that is, you go to sell pancakes, and then after that wait for the boss handed you do, you can take the process is synchronization. You go to KFC meal, then after the end point, a place to sit and play mobile phones, and other well after, the little brother little sister called the front desk to pick up your number notification process is asynchronous. Synchronization is only a piece of something to do, to finish a job, do one more. Rather than asynchronous, asynchronous process is waiting for you to do one thing, you can do other things. This is the distinction between synchronous and asynchronous.

the console.log ( . 1 ) 
the console.log ( ' sync ' ) 
the console.log ( 2 )
 // . 1
 // synchronization
 // 2
the console.log ( . 1 ) 
the setTimeout (() => {the console.log ( ' asynchronous')}, 0) 
the console.log ( 2 )
 // . 1
 // 2
 // asynchronous

 

Browser is single-threaded or multi-threaded? ---Multithreading

A browser typically have the following permanent thread:

- rendering engine thread: As the name suggests, this thread is responsible for rendering the page.

- JS engine: JS responsible for parsing and execution

- clocked flip-flop threads: process timed events, such as setTimeout, setInterval

- event triggers Thread: Handling DOM events

- asynchronous http request thread: http request processing

Js browser only be assigned to a main thread to perform the task, but can only perform one task, these tasks form a task execution queue waiting in line, however, certain tasks front end is very time consuming, if, let them and other tasks, are honest queued for execution, the efficiency will be very low, and even lead to suspended animation page. So, these time-consuming tasks to the browser, it opens up another thread, such as: http request, the browser timed triggers, etc. These tasks are asynchronous,

 That js single-threaded and asynchronous Is not it a paradox? Single-threaded and asynchronous really can not both be characteristic of a language, js chose to become a single-threaded language, so it can not in itself be asynchronous, but Js host browser, Node, etc. is multithreaded hosting environment in some way Js have such properties asynchronous.

Task Queue

js tasks into synchronous and asynchronous tasks tasks, task synchronization means that queued for execution on the main thread task, a task that only money is finished, before the execution of a task. Asynchronous task: do not enter the main thread, and enter the "task column" task only "task queue" notify the main thread, an asynchronous tasks can be performed. The task will enter the main thread.

Asynchronous operation mechanism for the implementation of:

- all synchronization tasks on the main thread to form a stack [Executive]

- outside the main thread, there is a "task queue." With asynchronous tasks run as long as a result, an event is placed in the "task queue" at home.

- Once the "execution stack" all kinds of synchronization task is finished, the system will read the "task queue" to see what events there are those corresponding asynchronous tasks, thus ending the wait state, into the execution stack, started.

- the main thread repeat the third step above.

Callback

When the main thread begins execution asynchronous task is to perform the corresponding callback function.

Asynchronous tasks must specify a callback function.

js in the asynchronous timer

setTimeout(function(){
  console.log(0);
},0)
 
console.log(1);
//1
//0

When there is time-consuming tasks, it will put jobs in the queue waiting for the main thread is idle before execution. The actual process is then performed, the browser will default setTimeout and ajax request this kind of methods are time-consuming procedure (although it may not be time-consuming). So at this time setTimeout although it off time is 0, but js does not happen immediately, but it added to the task queue, when the synchronization executing the task execution stack after printing is 1, then execute setTimeout callback function, print 0 .

setTimeout (fn, 0) meaning that specify a task execution on the main thread of the earliest available free time. Plan focuses ::: possible as soon as possible. That setTimeout callback function will be added to the current task queue, the current task takes too long to wait for a long time, there are ways to ensure that off, the callback function will be executed within a specified time.

 

Guess you like

Origin www.cnblogs.com/mn6364/p/10940102.html