Java Concurrent Programming: the origin of processes and threads

The foundation of Java multithreading: the origin of processes and threads

Having introduced the basic knowledge of Java, let's discuss a slightly more difficult problem: Java Concurrent Programming. Of course, Java Concurrent Programming involves many aspects, which can not be used overnight. It needs to be accumulated in practice. Since concurrency certainly involves multithreading, before entering the topic of concurrent programming, let's first understand the origin of processes and threads, which will be very helpful to the later understanding of concurrent programming.

The following is the table of contents for this article:

I Why do processes appear in the operating system?

II Why do threads occur?

III Multithreading concurrency

If there is anything wrong, please understand and welcome correction.

Please respect the author's labor achievements and indicate the original address for Reprint:

   http://www.cnblogs.com/dolphin0520/p/3910667.html

I Why do processes appear in the operating system?

Speaking of the origin of the process, we need to start with the development history of the operating system.

Perhaps today, we can't imagine what computers were like many years ago. Now we can use computers to do many things: office, entertainment and Internet access, but when computers first appeared, they were to solve the problem of mathematical calculation, because a lot of calculations are completed by manpower, which is very time-consuming and labor-intensive. At first, the computer can only accept some specific instructions. When the user inputs an instruction, the computer will do an operation. When the user is thinking or entering data, the computer is waiting. Obviously, this is inefficient and inefficient, because many times, the computer is waiting for user input.

So can you write down a series of instructions that need to be operated in advance, form a list, and then hand them over to the computer at one time, and the computer constantly reads the instructions for corresponding operations? In this way, the batch operating system was born. Users can write multiple programs to be executed on tape, and then hand them over to the computer to read and execute these programs one by one, and write the output results to another tape.

Although the birth of batch operating system has greatly improved the convenience of task processing, there is still a big problem:

If there are two tasks a and B, task a is in the middle of execution, A large number of data inputs (I / O operations) need to be read. At this time, the CPU can only wait quietly for task a to read the data before continuing to execute, which wastes CPU resources. People wonder whether task B can execute while task a reads the data. When task a reads the data, let Task B pause and then let task a continue to execute?

But there is a problem. It turns out that every time a program runs in the computer, that is, there is always only one program's running data in the memory. If you want task a to perform I / O operations and let Task B perform them, multiple programs must be loaded into memory. How to deal with it? How to distinguish the data used by multiple programs? And when a program is suspended, how can it return to its previous execution state?

At this time, people invented processes. They use processes to correspond to a program. Each process corresponds to a certain memory address space, and can only use its own memory space. Each process does not interfere with each other. And the process saves the running state of the program at each time, which makes it possible for process switching. When the process is temporary, it will save the status of the current process (such as process ID, resources used by the process, etc.). The next time it switches back, it will recover according to the previously saved status, and then continue to execute.

This is concurrency, which can make the operating system look like multiple tasks are executing in the same time period. In other words, processes make operating system concurrency possible.

Note that although there are multiple tasks executing in the macro view of concurrency, in fact, at any specific time, only one task is occupying CPU resources (of course, for a single core CPU).

II Why do threads occur?

After the emergence of processes, the performance of the operating system has been greatly improved. Although the emergence of process solves the concurrency problem of operating system, people are still not satisfied, and people gradually have requirements for real-time. Because a process can only do one thing in a time period, if a process has multiple subtasks, it can only execute these subtasks one by one. For example, for a monitoring system, it should not only display the image data on the screen, but also communicate with the server to obtain the image data, but also deal with people's interactive operation. If the system is communicating with the server to obtain image data at a certain time, and the user clicks a button on the monitoring system, the system will wait for the image data to be obtained before processing the user's operation. If it takes 10s to obtain the image data, the user will have to wait all the time. Obviously, people cannot be satisfied with such a system.

Can these subtasks be executed separately? That is, while the system acquires image data, if the user clicks a button, the acquisition of image data will be suspended, And respond to the user's operation first (because the execution time of the user's operation is often very short), after processing the user's operation, continue to obtain the image data. People invented threads to let one thread execute a subtask. In this way, a process includes multiple threads, and each thread is responsible for an independent subtask, so that when the user clicks the button, the acquisition of image data can be suspended To make the UI thread respond to the user's operation, and then switch back after the response, so that the thread obtaining the image can get CPU resources. Thus, users feel that the system is doing many things at the same time, which meets the requirements of users for real-time.

In other words, processes make the concurrency of the operating system possible, while threads make the internal concurrency of processes possible.

However, it should be noted that although a process includes multiple threads, these threads share the resources and address space occupied by the process. Process is the basic unit of resource allocation by the operating system, and thread is the basic unit of scheduling by the operating system.

III Multithreading concurrency

Since multiple threads share the resources and address space of their processes, there will be a problem:

What if multiple threads want to access a resource at the same time?

This problem is the synchronization problem to be focused on in the subsequent article.

Then a friend may ask that multithreading programming is often used now. Is the performance of multithreading necessarily due to single thread?

Not necessarily. It depends on the specific tasks and computer configuration. for instance:

For single core CPU, if it is a CPU intensive task, such as decompressing files, the performance of multithreading is not as good as that of single thread, because decompressing files needs to occupy CPU resources all the time. If multithreading is adopted, the overhead caused by thread switching will degrade the performance.

However, for interactive tasks, multithreading must be used

For multi-core CPUs, multithreading is certainly better than single thread for decompressing files, because multiple threads can make more full use of the resources of each core.

Although multithreading can improve program performance, its programming is much more complex than single thread, and thread safety should be considered. Therefore, in the actual programming process, it should be selected according to the actual situation.

About the origin of processes and threads, so much has been said for the time being. Interested friends can refer to relevant materials.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>