Tag:com over not -o ges validator process tor section
Java concurrent Programming: processes and Threads Table of Contents
- 1. What is a process?
- 2. What is a thread?
- 3. Why are there processes and threads?
- 4. Differences
1What is a process?
If we use the Task Manager, we know what the process is. Every time we open a program, we will create a new process.
The PID is the ID of the process, and a program corresponds to a process ID.
2What is a thread?
For example, in addition to the PID, there 线程 is another, the thread is working inside the process, such as the Emacs process PID is 20, the number of threads is 7, is in Emacs also ran 7 threads. Now you just need to understand the concept, and later, to explain it in detail.
3Why are there processes and threads?
In the earliest operating system, only one program can work, which is equivalent to a single-process system. For example, we are listening to the song now, but want to look at the mail, then, we need to first the song program to end. Surprisingly, the earliest operating system was single-tasking. Later, in order to realize the concurrent operation of the System program, do the time of the Shard processing. Is the CPU may be a, but the CPU running time unit into a small time slice, each program allocated a time slice run. Because the time slices are short and the number of programs is limited, the CPU will iterate through all the programs in a short period of time. The speed of the CPU is GHz, so we are not aware of it and feel that all the programs are running at the same time. In fact, strictly at the same time only one program is running. Therefore, the process makes it possible for different programs to run concurrently.
So, why is it wired? Thread is inside the process, that is, I have a program, for example, listen to the software, on the one hand it to play music, at the same time, it may also download other songs, or update the lyrics. Don't we have to download it, play it, turn it off, and then download it? Of course, that's what we can't stand. So, in the same program to achieve different tasks, we use the thread to achieve.
So, in general, there may be multiple processes in the operating system, and there may be multiple threads in each process, at least one main thread. A process is the unit in which the system allocates resources, and the thread is the unit of time allocated by the system. That is, each time we create a process, we allocate a certain amount of memory space to the process. A thread belongs to a process, so it can access all the memory space of the process, which is the memory space for multiple threads to share the process. The system allocates time slices based on the number of threads.
4Difference
Both the process and the thread are created to implement the concurrency of the task, so what are their differences, and when to use the process, and when to use the thread?
Although, they can all be implemented concurrently. But the overhead of creating them and the cost of communication are different. When they are different programs, it is certainly different process, because the program may be written by others, you want to make a thread is not possible. When you write a program, if we want to make a function of a separate module for different programs to call, then we will implement it as a new program. When used only within a program and requires access to resources in the current program, it can be implemented as a thread to facilitate the sharing of resources. But when it comes to multi-threading, and concurrent access to the same resource, it involves simultaneous access and modification of the same variable, data synchronization and consistency issues, this is also the most troublesome multithreaded programming, multithreading complexity and the likelihood of problems more than a single thread is much higher, this we will slowly explain the later.
Date:2017-07-02 21:10
Author:wen YANG
created:2017-07-03 Mon 21:23
Emacs 25.2.1 (ORG mode 8.2.10)
Validate
Java concurrent Programming: Processes and Threads