1. Hand-painted a picture description.
2. Why JavaScript is a single thread (quoted here as Nanyi teacher)
The single thread of JavaScript is related to its purpose.
As a browser scripting language, JavaScript's primary purpose is to interact with the user and manipulate the DOM.
This determines that it can only be single-threaded, otherwise it can lead to complex synchronization problems.
For example, assuming that JavaScript has two threads at the same time, one thread adds content to one of the DOM nodes, and the other thread deletes the node, which thread should the browser take precedence over?
So, to avoid complexity, JavaScript is a single thread from birth, which has become a core feature of the language and will not change in the future.
To take advantage of the computational power of multicore CPUs, HTML5 proposes a web worker standard that allows JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and must not manipulate the DOM.
So, this new standard does not change the nature of JavaScript single threading.
Portal: JavaScript operating mechanism in detail: Another talk about event Loop
What is the asynchronous representation of 3.JavaScript?
Like the beginning of the diagram, the individual thinks that the left main thread is synchronous, the left event queue (Message Queuing) is asynchronous.
Of course, there are many async in javascript:
- Ajax (XMLHttpRequest)
- Image Tag,script Tag,iframe (Similar principle)
- Settimeout/setinterval
- CSS3 transition/animation
- PostMessage
- Web Workers
- Web Sockets
- And more ...
Allen's blog summarizes several asynchronous ideas, portals: Asynchronous grooming in JavaScript (0)
JavaScript synchronous and asynchronous