Home >Java >JavaBase >The difference between java threads and processes

The difference between java threads and processes

(*-*)浩
(*-*)浩Original
2019-11-09 14:02:102978browse

The difference between java threads and processes

What is a process and what is a thread? (Recommended Learning: Java Course )

## :: It is the basic unit that distributes and manage resources during the execution process. It is a dynamic concept, competitive computer system resources the basic unit.

Thread: It is an execution unit of the process and an internal scheduling entity of the process. A basic unit that is smaller than a process and operates independently. Threads are also called lightweight processes.

A program has at least one process, and a process has at least one thread.

The difference between process threads

1. Address space: Threads in the same process share the address space of this process, while processes have independent address spaces.

2. Resource ownership: Threads in the same process share the resources of this process, but the resources between processes are independent.

3. After a process crashes, it will not affect other processes in protected mode, but if a thread crashes, the entire process will die. So multi-process is more robust than multi-threading.

4. When switching processes, a large amount of resources are consumed and the efficiency is high. So when it comes to frequent switching, it is better to use threads than processes. Similarly, if concurrent operations are required to be performed at the same time and share certain variables, you can only use threads and not processes.

5. Execution process: Each independent process has an entrance for program running, a sequential execution sequence and a program entrance. However, threads cannot execute independently and must exist in the application program, and the application program provides multiple thread execution control.

6. Threads are the basic unit of processor scheduling, but processes are not.

7. Both can be executed concurrently.

A thread only belongs to one process, but a process can have multiple threads, but at least one thread

Resources are allocated to a process, all in the same process Threads share all resources of the process.

Advantages and disadvantages:

Thread execution overhead is small, but it is not conducive to resource management and protection. Threads are suitable for running on SMP machines (dual CPU systems).

The process execution overhead is high, but it can manage and protect resources well. Processes can be moved forward across machines.

The above is the detailed content of The difference between java threads and processes. 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