1. Overview
In understanding threads (thread) We should first understand the process, these two are operating system concepts, more abstract difficult to understand, so I use my own understanding of these two concepts, but it is recommended to see the operating system related to the introduction of the book. 2. Single Task operating system
In ancient times (the last century), PCs were not yet popular, and computers were a scarce resource. At that time the operating system (such as Microsoft's DOS system), at a time can only handle one task, this is a single task operating system , and then people feel that this kind of cheap CPU, because sometimes in the I/O operation is the input and output, the CPU is not working. 3. Multi-task operating system
Then the multitasking operating system is on the scene, does it mean that the operating system can handle multiple tasks at the same time at one time? Not necessarily, if it is a single core CPU, a moment or in the process of a task, it's just that the CPU is making a quick switch between multiple tasks, switching faster, so you don't feel it switching, just like our naked eye doesn't see the light bulb flashing (because alternating current is changing size and direction rapidly). So, when you listen to the song while chatting QQ, do not think that they are running at the same time, but the short pause so that you do not feel. But when you have too many programs, it's obvious that the pause is clear. Single core CPU is working only one person in the fast switch to do the task, it is a person's ability is very limited after all, so this is why we compare the pursuit of multi-core cpu, people more power, you can have more than one person in the fast switch to do the task, the time of the pause is naturally short. In fact, we can derive the concept of parallelism (Parallelism) and concurrency (concurrency) , which is also an important concept, and we'll tell you in advance You can also look at the following concepts of processes and threads before you go back to it. 4. Concurrency and parallelism
When more than one thread is operating, if the computer has only one single core CPU, it is impossible to really do at the same time a thread above the processing, such as the following figure, red box representative in the processing, you will find two threads between the red box is not coincident with the place, this is the case of concurrency .
If the multi-core CPU, you can really do at the same time a thread above the processing, the situation may become the following, this is the case of parallelism .
So when a computer with a multi-core CPU or multiple CPUs is working on a Java program that has multiple threading operations, the thread operation may be parallel rather than concurrent. 5. Processes and Threads
Well, now it's time to talk about our processes and threads , but you can think of the tasks in the multitasking operating system above as processes. in a thread-supporting operating system, the process is used as the basic unit of resources, and the thread is used as the basic unit of dispatch and allocation . What does that mean, process is face, thread is lining (imitation of Wong Kar-wai's film "The Guru"), like a government project bidding, his project only to the company rather than to the individual, but the company's successful bid after the actual work of the company's employees, but the money is for the company, but the company is responsible for the employees pay. Speaking of which, I believe you have a bit of a vague idea. We can correspond to government projects-programs, companies-processes, employees-threads, money-resources. Here's a summary: a program needs at least one process , like a government project to outsource at least one company, sometimes multiple. a process needs at least one thread , like a company at least one employee process is the basic unit of resource allocation, the thread as the basic unit of dispatch and dispatch, the thread in the same process sharing resources , like the money that government projects send to the company, But the company is working for the employees, but the company is going to take the money to pay the employees.
For personal understanding, if there is anything bad or not correct, but also ask friends to enlighten.