Java Multithreading Fundamentals: The Origins of processes and threads

Source: Internet
Author: User

Original address:

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

--------------------------------------------------------------------------------------------------------------- ----------------------------------------------------

In the previous article, we've covered the basics of Java, and now we're going to talk a little bit more difficult: Java concurrency programming. Of course, Java concurrent programming involves many aspects of content, not overnight can be used to digest, need to accumulate in practice. Since concurrency is definitely involved in multithreading, before entering the topic of concurrent programming, let's take a look at the origins of processes and threads, which will be of great help in the subsequent understanding of concurrency programming.

The following is the directory outline for this article:

I. Why is there a process in the operating system?

Two. Why are threads present?

Three. Multithreading concurrency

If there is any difference, please understand and welcome correct.

I. Why is there a process in the operating system?

Talking about the origin of the process, we need to start with the development of the operating system.

Perhaps today, we can't imagine what a computer would look like many years ago. We can now use the computer to do a lot of things: office, entertainment, Internet, but when the computer just appeared, is to solve the problem of mathematical computing, because a lot of computation through the manpower to complete the time and labor costs. In the beginning, the computer can only accept some specific instructions, the user input a command, the computer to do an operation. When the user is thinking or entering data, the computer is waiting. Obviously this is efficient and very low, because many times the computer is waiting for the user to enter the state.

Then can you write down a series of instructions that need to be manipulated, form a list, and then hand it over to the computer, and the computer constantly reads the instructions to perform the corresponding operation? In this way, the batch processing operating system was born. Users can write multiple programs that need to be executed on tape and then leave them to the computer to read and execute the programs one after the other, and write the output to another tape.

Although the birth of the batch operating system greatly improves the convenience of task handling, there is still a big problem:

If there are two tasks A and B, task A will need to read a large amount of data input (I/O operations) during the execution of the process, while the CPU can only wait silently until task a reads the data to continue execution, wasting CPU resources. People would like to be able to read the data in task A in the process, let task B to execute, when task a read the data, let task B pause, and then let task a continue to execute?

But then there is a problem, the original every time a program in the computer running, it is said in memory always only one program running data. And if you want task A to perform I/O operations, let task B go to execute, it is necessary to load multiple programs in memory, then how to deal with it? How does the data used by multiple programs be identified? And when a program is paused, how does it revert back to the state it was executing before?

This time people invented the process, with a process to correspond to a program, each process corresponds to a certain amount of memory address space, and only use its own memory space, each process between each other. And the process saves the running state of every moment of the program, which provides the possibility for process switching. When the process is transient, it saves the state of the current process (such as the process identity, the resources used by the process, etc.), and, on the next re-switch back, restores it based on the previously saved state, and then resumes execution.

  This is concurrency, which allows the operating system to appear on a macro scale with multiple tasks executing at the same time period . In other words, a process makes it possible for the concurrency of the operating system.

Note that although there are multiple tasks on the macro level, in fact, at any given moment, only one task is taking up CPU resources (of course, for a single-core CPU).

Two. Why are threads present?

After the process has occurred, the performance of the operating system has been greatly improved. Although the development of the process solves the concurrency problem of the operating system, people are still not satisfied, people gradually have a demand for real-time. Because a process can only do one thing in a period of time, if a process has more than one subtask, you can only perform these subtasks on a per-task basis. For example, for a monitoring system, it not only to display the image data on the screen, but also to communicate with the server to obtain image data, but also to deal with people's interactive operation. If a moment the system is communicating with the server to obtain image data, and the user in the monitoring system to click on a button, then the system will wait to obtain the image data before processing the user's operation, if the acquisition of image data needs to consume 10s, then the user only has been waiting. Obviously, for such a system, people are not satisfied.

Can these subtasks be executed separately? That is, when the system obtains the image data, if the user clicks on a button, it pauses the acquisition of the image data, and first responds to the user's action (because the user's actions are often executed in a short time), after processing the user operation, and then continue to obtain the image data. People invented the thread, let a thread to perform a subtask, so that a process consists of multiple threads, each thread is responsible for a separate sub-task, so that when the user clicks on the button, you can pause the acquisition of the image data thread, let the UI thread respond to the user's actions, and then switch back after the response, Let the thread getting the image get CPU resources. So that users feel that the system is at the same time doing more things, to meet the users of real-time requirements.

In other words, a process makes the concurrency of the operating system possible, and threads make internal concurrency of the process possible.

Note, however, that while a process consists of multiple threads, these threads share the resources and address space that the process occupies. A process is the basic unit for an operating system to allocate resources, and a thread is the basic unit for operating system scheduling.

Three. Multithreading concurrency

Because multiple threads are collectively occupying the resource and address space of the owning process, there is a problem:

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

This question is the synchronization problem to be emphasized in the post-order article.

Then there may be friends will ask, now many times are using multi-threaded programming, then is not multithreaded performance must be due to single-threaded it?

Not necessarily, it depends on the specific tasks and the configuration of the computer. For example:

  For a single-core CPU, if it is CPU-intensive tasks, such as extracting files, multithreading performance is not as single-threaded performance, because the extracted files need to occupy CPU resources, if the use of multi-threading, the cost of thread switching will cause performance degradation.

  However, for tasks such as interaction types, it is definitely necessary to use multi-threaded ,

  For multicore CPUs, multithreading is certainly better than a single thread for extracting files, because multiple threads can take advantage of the resources of each core more fully.

While multi-threading can improve program performance, it is much more complex to program than a single thread, taking into account threading security issues. Therefore, in the actual programming process, according to the actual situation of the specific choice.

About the origin of the process and thread, for the time being so much, interested friends can refer to the relevant information.

Java Multithreading Fundamentals: The Origins of processes and threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.