Asynchronous synchronization mechanisms in JavaScript and thread-deep understanding of the underlying

Source: Internet
Author: User

Today on the Internet to see a variety of JS asynchronous synchronous single-threaded multi-threaded discussion after the baptism of the predecessors and my humble understanding of the small to come up with an armchair ~

JS itself is a single-threaded as to why JS is a single-threaded, that will go back to the history of JS, in short, because JS is the browser's scripting language often operate DOM elements of multi-threaded words will lead to more complex (delete and add at the same time?) This is simply explained why JS to single-threaded reason, Why can JS also execute the method asynchronously? This special is not very contradictory!!!

However, but note that because JS is mainly run in the browser script language browser is a typical GUI worker because it is in most environments to implement event processing, in order to avoid synchronization caused by the blocking interaction so it produces asynchronous characteristics (here or the role of the browser and JS itself is not related to the main thread )

JS single-threaded at the same time in the browser only a piece of code in the execution, and the asynchronous mechanism is implemented by the browser, by the browser's two or more resident threads to do together, and the main task or JS single-threaded to implement the worker thread just another open up a task queue This task queue will store all the results of asynchronous method execution, and when the main thread stack of JS is fully executed, it will go to the task queue to find out if there are any tasks to execute if there is a stacks to plug it into the tail of the main line after execution and then continue the loop (Event loop What about the idea of saying so much more than a chestnut?

Common asynchronous methods such as timers this is the most common!!

This example simply explains the next asynchronous execution sequence (the asynchronous execution method is always in the back of the JS main thread)

Open the console

Console.log (' I was the first one to appear!! ‘)

SetTimeout (function () {

Console.log (' I was the third one to appear!! ‘)

},0)

Console.log (' I was the second one to appear!! ’)

Console will output I was the first one to appear!!

I was the second one to appear!!

I was the third one to appear!!

This is because the timer has a separate thread in the browser that is not part of the main threads

Asynchronous synchronization mechanisms in JavaScript and thread-deep understanding of the underlying

Related Article

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.