JS single-threaded and asynchronous

The concept of single-threaded and thread:

Thread: is the smallest unit of an operating system capable of operation scheduling. It is included in the process, the actual operation of the unit process. A thread refers to a single control flow of a process sequence, a process can be complicated by a plurality of threads, each thread in parallel to perform different tasks.

:( single thread above have said: a thread refers to a single sequential flow of control process) is only one thread in the process, during program execution, the program path to go down the row in consecutive order, the front must deal with well, the back will be performed. .

 

Why JS is single-threaded?

Quoted Ruan Yifeng teacher's answer ---> JavaScript is single-threaded, and its related purposes. As a browser scripting language, JavaScript main purpose is to interact with the user, as well as operating DOM. This determines that it can only be a single-threaded, otherwise it will bring a very complex synchronization problems. For example, suppose JavaScript while there are two threads, one thread to add content on a DOM node, another thread to delete this node, then the browser which thread should prevail?

So, to avoid complexity, from the birth, JavaScript is single-threaded.

 

js working mechanism:

When the premise of the thread does not perform any synchronization code will execute asynchronous code, setTimeout is asynchronous code, can only wait so setTimeout js idle will be implemented, but infinite loop is never idle, it will never perform setTimeout . Even setTimeout 0, after he and other js engine is executing the code will be inserted into the final execution js engine threads.

 

Browser kernel mechanisms:

The browser kernel is multithreaded , a browser is generally achieved at least three permanent threads:

1. javascript engine : a single-threaded event-driven execution, JS engine has been waiting for the arrival of a task queue task and then be dealt with, no matter what time the browser is only one thread running JS JS-based program.

2. GUI rendering thread : responsible for rendering browser interface, when the interface needs to rearrange or redraw operation caused due to some kind of reflux, the thread will execute. But note GUI rendering thread and JS engine are mutually exclusive, when the JS engine executes the GUI thread is suspended, GUI updates are saved to be executed immediately when the engine idle until the JS in a queue.

3. Event Trigger thread : When an event is triggered thread will add the event to the pending end of the queue, waiting to be processed JS engine. These events can come from other threads currently executing JavaScript engine block of code such as setTimeOut, but also from browser kernel, such as a mouse click, AJAX asynchronous requests, etc., but because of the single-threaded relationship JS All of these events had to wait in line JS engine.

 

Single-threaded and asynchronous 

About JavaScript seen a lot of single-threaded and asynchronous, most have cited examples to illustrate this category: single thread is queued after a task is not completed before a start can not be, it is often said serial. Restaurant meal, everyone line up, after A customer end point, the kitchen started to do, done after the meal received A, B and then began ordering the kitchen and began to do ... again and again, which is synchronized meal when the kitchen is idle, when cooking the foreground is idle. That is single-threaded. Everyone can understand it, this efficiency is too low!

  A change in thinking, point A complete meal, aside waiting, the waiter handed the menu the kitchen, the kitchen started, and then continue to serve the customer B, when A customer good meal, A to insert the ranks of teams, the meal Take it. Hearing this, a lot of high-efficiency Well, the restaurant did not waste a bit of work force, we all divisions incident. Then authors came to the conclusion, which is single-threaded and asynchronous JavaScript.

 

On single-threaded and asynchronous temporarily on the first record of these things, follow-up will complement other like event loop and other related knowledge. If the content is inaccurate, we welcome the valuable advice ~~~

Guess you like

Origin www.cnblogs.com/wangrenmeng/p/10954805.html