Home > Web Front-end > JS Tutorial > body text

What thread is javascript

青灯夜游
Release: 2021-11-04 17:40:30
Original
1630 people have browsed it

Javascript is single-threaded. As a browser scripting language, the main purpose of JavaScript is to interact with users and operate the DOM; this determines that it can only be single-threaded, otherwise it will cause very complex synchronization problems.

What thread is javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

What is a thread in JavaScript? Why?

A major feature of the JavaScript language is single-threading, which means that only one thing can be done at the same time. So why can't JavaScript have multiple threads? This can improve efficiency.

The single thread of JavaScript is related to its purpose. As a browser scripting language, JavaScript's main purpose is to interact with users and manipulate the DOM. This determines that it can only be single-threaded, otherwise it will cause very complex synchronization problems. For example, suppose JavaScript has two threads at the same time. One thread adds content to a certain DOM node, and the other thread deletes the node. In this case, which thread should the browser use?

So, in order to avoid complexity, JavaScript has been single-threaded since its birth. This has become the core feature of this language and will not change in the future.
In order to take advantage of the computing power of multi-core CPUs, HTML5 proposes the Web Worker standard, which allows JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and must not operate the DOM. Therefore, this new standard does not change the single-threaded nature of JavaScript.

Processes and threads

1. Process

A process refers to an execution of a program, which occupies a unique memory space. You can view the process through Windows Task Manager (as shown below). At the same time, the same computer system allows two or more processes to be in parallel state, which is multi-process. For example, a computer can run WeChat, QQ, and various browsers at the same time. Some browsers run in a single process, such as firefox and the old version of IE, and some run in multiple processes, such as chrome and the new version of IE.

2. Thread

Some processes can do more than one thing at the same time, such as Word, which can perform typing, spell checking, printing and other things at the same time. Within a process, if you want 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 threads.
Thread refers to the basic scheduling unit of the CPU, a complete process of program execution, and an independent execution unit within the process. Multithreading refers to having multiple threads running at the same time within a process. The browser runs multi-threaded. For example, you can use a browser to download, listen to songs, and watch videos at the same time. In addition, we need to know that a major feature of JavaScript language is single-thread. In order to utilize the computing power of multi-core CPU, HTML5 proposes the Web Worker standard, which allows JavaScript scripts to create multiple threads, but child threads are completely subject to The main thread controls and must not manipulate the DOM. Therefore, this new standard does not change the single-threaded nature of JavaScript.

Since 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 be executed at the same time. The execution method of multi-threading is the same as that of multiple processes. The operating system also quickly switches between multiple threads, allowing each The threads all alternately run briefly and appear to be executing simultaneously. Of course, truly executing multiple threads simultaneously requires a multi-core CPU to be possible.

3. Processes and threads

  • The application must run on a thread of a certain process
  • At least one thread in a process A running thread: the main thread, which is automatically created after the process starts.
  • If multiple threads are running in a process at the same time, then the program is multi-threaded.
  • The memory space of a process is shared , each thread can use these shared memories.
  • Data cannot be shared directly between multiple processes

4. What are the advantages and disadvantages of single-threading and multi-threading?

Advantages of single-threading: Sequential programming is simple and easy to understand

Disadvantages of single-threading: Low efficiency

Advantages of multi-threading: Can effectively improve CPU utilization

Disadvantages of multi-threading:

  • The overhead of creating multi-threads
  • The overhead of switching between threads
  • Deadlock and state synchronization issues

[Recommended learning: javascript advanced tutorial]

The above is the detailed content of What thread is javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template