Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the concept of JavaScript single thread

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly introduces "what is the concept of JavaScript single thread". In daily operation, I believe many people have doubts about the concept of JavaScript single thread. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "what is the concept of JavaScript single thread?" Next, please follow the editor to study!

In JavaScript, because the main purpose is to interact with users and operate DOM, only one thing can be done at the same time, which determines that it can only be single-threaded, otherwise it will bring very complex synchronization problems. In order to avoid complexity, JavaScript has been single-threaded since its birth.

The operating environment of this tutorial: windows10 system, javascript1.8.5 version, Dell G3 computer.

Why is JavaScript single thread?

One of the major features of the JavaScript language is single-threading, that is, you can only do one thing at a time.

The single thread of JavaScript is related to its purpose.

As a browser scripting language, the main purpose of JavaScript is to interact with users and to manipulate DOM.

This determines that it can only be single-threaded, otherwise it will bring very complex synchronization problems. In order to take advantage of the computing power of multicore CPU, HTML5 proposes the Web Worker standard, which allows JS scripts to create multiple threads, but child threads are completely controlled by the main thread and cannot operate DOM. Therefore, this new standard does not change the nature of JS single-threading.

For example, suppose JavaScript has two threads at the same time

A thread adds content to a DOM node

Another thread deleted the node

Which thread should the browser follow at this time?

So, to avoid complexity, JavaScript has been single-threaded since its birth, which has become a core feature of the language and will not change in the future.

Task queue

Single threading means that all tasks need to be queued and the previous task ends before the next task is executed. If the former task takes a long time, the latter task will have to wait all the time.

If the queue is because of a large amount of computation, CPU is too busy, but most of the time CPU is idle, because IO devices (input and output devices) are very slow (for example, Ajax operations read data from the network) and have to wait for the results to come out before moving on.

The designers of the JavaScript language realized that at this point the main thread could completely ignore the IO device, suspend the waiting task, and run the next task first. Wait until the IO device returns the result, then go back and continue the pending task.

Therefore, all tasks can be divided into two types, one is synchronous task (synchronous), the other is asynchronous task (asynchronous). A synchronous task means that a task queued on the main thread can only be executed after the previous task has been executed; an asynchronous task refers to a task that enters the "task queue" (task queue) instead of entering the main thread, and only when the "task queue" notifies the main thread that an asynchronous task can be executed.

Specifically, the operating mechanism of asynchronous execution is as follows. The same is true of synchronous execution, because it can be seen as asynchronous execution without asynchronous tasks. )

(1) all synchronization tasks are executed on the main thread, forming an execution stack (execution context stack).

(2) besides the main thread, there is a "task queue" (task queue). As long as the asynchronous task has a run result, place an event in the Task queue.

(3) once all the synchronization tasks in the execution stack are completed, the system will read the Task queue to see what events are in it. Those corresponding asynchronous tasks then end the waiting state, enter the execution stack, and begin execution.

(4) the main thread repeats the third step above.

The following figure is a schematic diagram of the main thread and task queue.

At this point, the study of "what is the concept of JavaScript single thread" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report