Home>Article>Web Front-end> What is the difference between processes and threads in JavaScript

What is the difference between processes and threads in JavaScript

Robert De Niro
Robert De Niro Original
2021-06-09 17:13:04 2940browse

Difference: The process has an independent address space. After a process crashes, it will not affect other processes in protected mode; threads are just different execution paths in a process (a process consists of one or more Composed of threads), there is no separate address space (shared memory) between threads. The death of one thread is equivalent to the death of the entire process.

What is the difference between processes and threads in JavaScript

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

Threadsare divided into: single thread and multi-thread

Single thread: A running program (that is, running) has at least one thread, This thread is called the main thread. A program with only one main thread is called a single-threaded program. The main thread is responsible for executing all codes (UI display and refresh, network requests, local storage, etc.). These codes can only be executed sequentially and cannot be executed concurrently.

Multi-threading: A program with multiple threads is called a multi-threaded program. The main thread can open up multiple sub-threads. The sub-threads and the main thread are independent running units. Their respective executions do not affect each other and can be executed concurrently. .

The difference between single thread and multi-thread:

Single thread: There is only one thread, the code is executed sequentially, and code blocking (page suspended animation is prone to occur) );

Multi-threading: Having multiple threads and running independently between threads can effectively avoid code blocking and improve the running performance of the code.

ProcessDefinition:A process is an ongoing program, it is a dynamic concept. It is the basic unit for resource allocation and scheduling in the system.

The difference between processes and threads:

A program includes at least one process, and a process includes at least one thread;

Multi-processes have independent memory, and multi-threads share memory, so multi-threading improves operating efficiency;

The importance of multi-threading is that multiple programs can be executed at the same time, but the system does not regard multi-threading as into multiple independent applications.

In-depth understanding:

Example:

1. The core of the computer is the CPU, which is responsible for All computing tasks. It's like a factory, running all the time.

2. Assume that the factory’s power is limited and can only be supplied to one workshop at a time. In other words, when one workshop starts working, other workshops must stop working. The meaning behind it is that a single CPU can only run one task at a time.

3. A process is like a factory floor, it represents a single task that the CPU can handle. At any time, the CPU is always running one process, and other processes are in a non-running state.

4. There can be many workers in a workshop. They work together to complete a mission.

5. Threads are like workers in a workshop. A process can include multiple threads.

6. The space in the workshop is shared by workers. For example, many rooms can be entered and exited by every worker. This symbolizes that the memory space of a process is shared, and each thread can use these shared memories.

7. However, the size of each room is different, and some rooms can only accommodate one person at most, such as the toilet. When there are people inside, no one else can go in. This means that when a thread uses some shared memory, other threads must wait for it to end before they can use this memory.

8. A simple way to prevent others from entering is to add a lock to the door. Those who arrived first locked the door, and those who arrived later saw the lock and lined up at the door, waiting for the lock to open before entering. This is called"Mutual exclusion lock"(Mutual exclusion, abbreviated as Mutex), which prevents multiple threads from reading and writing a certain memory area at the same time.

9. There are also some rooms that can accommodate n people at the same time, such as the kitchen. In other words, if the number of people is greater than n, the extra people can only wait outside. This is like some memory areas that can only be used by a fixed number of threads.

10. The solution at this time is to hang n keys at the door. The person who goes in takes a key and hangs the key back up when he comes out. Those who arrived later found that the keys were empty, so they knew they had to wait in line at the door. This approach is called"Semaphore"(Semaphore), which is used to ensure that multiple threads will not conflict with each other. It is not difficult to see that mutex is a special case of semaphore (when n=1). In other words, the latter can completely replace the former. However, because mutex is relatively simple and efficient, this design is still used when resource exclusivity must be guaranteed.

11. The design of the operating system can be boiled down to three points:

(1) In the form of multi-process, multiple tasks are allowed to run at the same time;

(2) In the form of multi-threading, a single task is allowed to be divided into different parts to run; share resources among.

The main difference between processes and threads is that they are different operating system resource management methods. The process has an independent address space. After a process crashes, it will not affect other processes in protected mode, and threads are just different execution paths in a process.

Threads have their own stacks and local variables, but there is no separate address space between threads. The death of one thread means the death of the entire process, so multi-process programs are more robust than multi-threaded programs, but When switching processes, more resources are consumed and the efficiency is lower.

But for some concurrent operations that require simultaneous operation and sharing of certain variables, only threads, not processes, can be used.

For more programming related knowledge, please visit:
Programming Video

! !

The above is the detailed content of What is the difference between processes and threads in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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