Home  >  Article  >  Java  >  The difference between threads and processes in java

The difference between threads and processes in java

王林
王林Original
2019-12-03 15:56:192691browse

The difference between threads and processes in java

1. Definition

A process is a program with certain independent functions. Regarding a running activity on a certain data collection, a process is a system that allocates and schedules resources. independent unit.

Thread is an entity of the process and the basic unit of CPU scheduling and dispatch. It is a basic unit that is smaller than the process and can run independently. The thread itself basically does not own system resources, only a few resources that are essential for running (such as a program counter, a set of registers and a stack), but it can share all the resources owned by the process with other threads belonging to the same process. .

2. Relationship

A thread can create and cancel another thread; multiple threads in the same process can execute concurrently.

Relative to a process, a thread is a concept closer to an execution body. It can share data with other threads in the same process, but has its own stack space and an independent execution sequence.

Recommended online video tutorial: java video tutorial

3. Difference

The main difference between processes and threads is that they are different operating system resource management Way.

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. However, for some concurrent operations that require simultaneous operation and sharing of certain variables, only threads, not processes, can be used.

1) In short, a program has at least one process, and a process has at least one thread.

2) The division scale of threads is smaller than that of processes, which makes multi-threaded programs have high concurrency.

3) In addition, the process has an independent memory unit during execution, and multiple threads share memory, thus greatly improving the running efficiency of the program.

4) There are differences between threads and processes during execution. Each independent thread has an entry point for program execution, a sequential execution sequence, and an exit point for the program. However, threads cannot execute independently and must exist in the application program, and the application program provides multiple thread execution control.

5) From a logical point of view, the meaning of multi-threading is that in an application, multiple execution parts can be executed at the same time. However, the operating system does not regard multiple threads as multiple independent applications to implement process scheduling and management and resource allocation. This is the important difference between processes and threads.

Recommended related articles and tutorials: Getting started with java

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