In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-07 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "what thread is javascript". In daily operation, I believe many people have doubts about what thread javascript is. The editor consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the question of "what thread is javascript?" Next, please follow the editor to study!
Javascript is single-threaded. As a browser scripting language, the main purpose of JavaScript is to interact with users and to manipulate DOM;, which determines that it can only be single-threaded, otherwise it will bring complex synchronization problems.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
What thread is JavaScript? Why?
One of the major features of the JavaScript language is single threading, that is, you can only do one thing at a time. So why can't JavaScript have multiple threads? This will improve efficiency.
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. For example, suppose JavaScript has two threads at the same time, one thread adds content to a DOM node and the other thread deletes the node, which thread should the browser follow?
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.
In order to take advantage of the computing power of multicore CPU, HTML5 proposes the Web Worker standard, which allows JavaScript 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 JavaScript single-threading.
Processes and threads 1. Process
A process is an execution of a program that occupies a unique memory space and can be viewed through the windows task manager (see figure below). Two or more processes are allowed to be in parallel in the same computer system at the same time, which is multiple processes. For example, computers run Wechat, QQ, and various browsers at the same time. Some browsers run as single processes, such as firefox and the old version of IE, and some are multi-processes, such as chrome and the new IE.
two。 Thread
Some processes do more than one thing at the same time, such as Word, which can type, spell check, print, and so on. Within a process, to do multiple things at the same time, you need to run multiple "subtasks" at the same time. We call these "subtasks" within the process Thread.
Thread refers to the basic scheduling unit of CPU, a complete process of program execution, and an independent execution unit within the process. Multithreading means that multiple threads are running in a process at the same time. The browser runs multithreaded. For example, use the browser to download, listen to music and watch videos at the same time. In addition, we need to know that one of the major features of the JavaScript language is single thread. in order to make use of the computing power of multi-core CPU, HTML5 puts forward the Web Worker standard, which allows JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and cannot operate DOM. Therefore, this new standard does not change the nature of JavaScript single-threading.
Because each process has to do at least one thing, a process has at least one thread. Of course, a complex process like Word can have multiple threads, and multiple threads can execute at the same time, and multi-threads execute in the same way as multi-processes, and the operating system switches quickly between multiple threads, allowing each thread to run alternately briefly, as if it were executing at the same time. Of course, it takes a multi-core CPU to actually execute multiple threads simultaneously.
3. Processes and threads
The application must run on a thread of a process
There is at least one running thread in a process: the main thread, which is automatically created after the process starts.
If multiple threads are running in a process at the same time, the program is multithreaded.
The memory space of a process is shared, and each thread can use the shared memory.
Data between multiple processes cannot be shared directly
4. What are the advantages and disadvantages of single-thread and multi-thread?
Advantages of single threading: sequential programming is easy to understand
Disadvantage of single thread: low efficiency
The advantage of multithreading: it can effectively improve the utilization of CPU
Disadvantages of multithreading:
Create multithreaded overhead
Inter-thread switching cost
Deadlock and state synchronization problem
At this point, the study on "what thread javascript is" 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.