Thread Overview
Threads and processes
Process Features
Concurrency and Parallelism
Advantages of Multithreading
1. Thread Overview
Almost all operating systems support running multiple tasks at the same time. A task is usually a program, and each running program is a process.
When a program runs, it may contain multiple sequential execution streams. Each sequential execution stream is a thread.
2. threads and processes
Almost all operating systems have the concept of processes, and all running tasks usually correspond to one process. When a program runs in the memory, it is a process.
A process is a running program with independent capabilities. A process is an independent unit for system resource allocation and scheduling.
Process features:
A. Independence: A process is an independent entity in the system. It can have its own independent resources and each process has its own private address.
Without the permission of the process itself, a user process cannot access other process address spaces.
B. Dynamic: the difference between a process and a program is that a program is only a set of static commands, and a process is a set of commands that are active in the system.
The concept of time is added to a program. A process has its own life cycle and various States. These concepts are not available to the program.
C. Concurrency: multiple processes can be executed concurrently on a single processor without affecting each other.
Multithreading extends the concept of multi-process, so that the same process can process multiple tasks concurrently. A thread is also called a Lightweight Process. A thread is the execution unit of a Process.
Like a process in an operating system, a thread is an independent and concurrent execution stream in a program. After the process is initialized, the main thread is created. For most applications, only one main thread is required,
However, we can also create multiple sequential execution streams in the process. These sequential execution streams are threads, and each thread is independent of each other.
A thread is a component of a process. A process can have multiple threads. A thread must have a parent process. A thread can have its own heap, stack, program counter, and local variables, but cannot have system resources,
It shares all resources of the process with other threads of the parent process. Because multiple threads share all the resources of the parent process.
A thread can complete certain tasks, share the variables and some environments in the parent process with other threads, and collaborate with each other to complete the tasks to be completed by the process.
The thread runs independently and does not know whether other processes exist in the process. Thread execution is preemptible, that is, the currently running thread can be suspended at any time for other threads to run.
One thread can create and withdraw another thread, and multiple threads in the same process can execute concurrently.
Summary: there is at least one process after a program runs. A process can contain multiple threads. Contains at least one thread.
3. Concurrency and Parallelism
Concurrency and parallel are two concepts;
Parallelism refers to the simultaneous execution of multiple commands (threads) on multiple processors at the same time;
Concurrency means that only one instruction (thread) can be executed at a time, but multiple process commands are quickly rotated for execution, so that multiple processes can be executed simultaneously at a macro level.
4. Advantages of Multithreading
The thread division scale is smaller than the process, making multi-thread Division highly concurrent. The process has its own independent units during execution, and multiple threads share the memory, thus improving the running efficiency.
Threads have higher performance than processes. This is because threads in the same process share the same virtual space.
Common environments of threads include process code segments and process data. Threads can easily communicate with each other using common data.
When an operating system creates a process, the process must have independent memory space and allocate a large amount of related resources. However, it is much easier to create a thread, therefore, the performance of multi-thread concurrency is much higher than that of multi-process concurrency.
Advantages of multithreading:
A. Memory cannot be shared between processes, but it is very easy to share memory between threads.
B. The system creation process needs to re-allocate system resources to the process, but it takes much less to create a thread. Therefore, it is more efficient to use a thread to implement multi-task concurrency than multi-process.
C. Java has built-in multi-threaded functions, rather than simply being used as the scheduling method of the underlying operating system, thus simplifying Java's multi-threaded programming.