One, process and thread
1. Process
When a program goes into memory run, it becomes a process. A process is a program that is in the process of running. A process is an independent unit of the operating system for resource allocation and scheduling. Three characteristics of a process:
Entities that exist independently, each with its own private piece of memory space.
A program is just a static set of instructions, and a process is a collection of instructions that are active in the system.
Multiple processes can execute concurrently on a single processor.
2. Threads
A thread is the smallest unit that the operating system can perform operations on, which is included in the process and is the actual operating unit of the process. Threads are also known as lightweight processes. Threads are independent, concurrent execution streams in the process.
3. The difference between threads and processes
Threads are part of a process, a process can have many threads, and each thread performs different tasks in parallel. Different processes use different memory spaces, and threads share all of the resources owned by the parent process with other threads of the parent process. It's easy to program, but be more careful. Do not confuse the memory space with the stack memory, each with a separate stack memory to store the local data. Threads have their own stacks, their own program counters, and their own local variables, but do not own system resources. The scheduling and management of threads is done by the process itself. The operating system schedules, manages, and allocates resources to the process.
4. Concurrency and parallelism
Concurrency means that only one instruction can be executed at the same point in time, but multiple process instructions are executed quickly, making it possible for the macro to have more than one process concurrently executing the effect.
Parallel means that at the same point in time, multiple instructions are executed concurrently on multiple processors
Second, the advantages of multithreading
Memory cannot be shared between processes, but memory is shared between threads. The system needs to reallocate system resources for the process when it is created, but it is much less expensive and efficient to create threads. Multithreading has the following advantages:
- Better resource utilization
- Programming is easier in some cases
- Faster program Response
JAVA Multithreading and Concurrency Learning notes (i)