Java concurrent programming principle and practice three: Multi-threading and multi-process connection and the problem of resource wasting caused by context switch

Source: Internet
Author: User

First, the process

Consider a scenario: browser, NetEase cloud music and notepad++ three software can only be executed sequentially is what kind of scene? In addition, if there are two programs A and B, program 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, thus wasting CPU resources. Have you ever thought that while program a reads the data, let program B go, and when program a reads the data, let program B pause. Smart, of course it's okay, but here's a keyword: toggle.

Since it is switching, then this involves state saving, state recovery, plus program A and program B required system resources (memory, hard disk, keyboard, etc.) are not the same. There is a natural need to have something to record what the program A and program B need, how to identify program A and program B, etc. (such as reading).

Process definition:

A process is a dynamic execution of a program on a data set. The process is generally composed of three parts: program, data set and process control block. The program we write is used to describe what the process is going to accomplish and how it is done, and the data set is the resource that the program needs to use in its execution, and the process control block is used to document the external characteristics of the process, to describe the process of its execution, and it can be used to control and manage the process, which is the only sign of

One example illustrates the process:
Imagine a computer scientist with a good hand in cooking is baking a birthday cake for his daughter. He has recipes for birthday cakes, ingredients for the kitchen: flour, eggs, sugar, vanilla juice, etc. In this analogy, the recipe for making a cake is a program (that is, an algorithm that is described in the proper form), and the computer scientist is the processor (CPU), and the raw material that makes the cake is the input data. The process is the sum of a series of actions that the chef reads recipes, takes a variety of ingredients, and bakes cakes. Now suppose the son of a computer scientist came in crying and said his head was stung by a bee. Computer scientists record what he did to the recipe (save the current state of the process), and then take out a first aid manual and follow the instructions to treat the sting. Here, we see the processor switching from one process (making a cake) to another high-priority process (implementing medical treatment), each with its own program (recipes and First aid manuals). When the Bee sting was finished, the computer scientist came back to make the cake and went on from the step he left.

Second, the thread

The occurrence of the thread is to reduce the consumption of the context switch, improve the concurrency of the system, and break through a process can only do the same thing, so that in-process concurrency becomes possible.

Suppose that a text program needs to accept keyboard input, display the content on the screen, and save the information to the hard drive. If there is only one process, it is bound to cause the same time only the embarrassment of doing the same thing (when saved, you can not enter content through the keyboard). If there are multiple processes, each process is responsible for a task, process A is responsible for receiving keyboard input tasks, and process B is responsible for displaying the content on the screen, and process C is responsible for saving the contents to the hard disk. The collaboration between the process a,b,c involves the process communication problem, and there is something that we all need to have ——-text content, constantly switching to cause loss of performance. If there is a mechanism, you can make the task A,b,c share resources, so that context switches need to save and restore less content, but also reduce the performance of the communication caused by the loss, it is good. Yes, this mechanism is a thread.
A thread is also called a lightweight process, which is a basic CPU execution unit and the smallest unit in a program's execution, consisting of a thread ID, a program counter, a register collection, and a stack. The introduction of threads reduces the overhead of program concurrency and improves the concurrency performance of the operating system. The thread does not have its own system resources.

Iii. relationship of processes and threads

Process is a computer program on a set of data on a running activity, the system is the basic unit of resource allocation and scheduling, is the basis of the operating system structure. Or a process is a program with a certain set of independent functions on a data collection of a running activity, the process is the system for resource allocation and scheduling of an independent unit.
A thread is an entity of a process that is the basic unit of CPU dispatch and dispatch, which is a smaller unit that can run independently than a process.

Relationship of process and thread:

(1) A thread can belong to only one process, while a process may have multiple threads, but at least one thread.
(2) A resource is allocated to a process, and all the threads of the same process share all the resources of that process.
(3) The CPU is assigned to a thread, that is, a thread that actually runs on the CPU.

Iv. Parallelism and concurrency

Parallel processing (Parallel processing) is a computational method that can perform two or more processes simultaneously in a computer system. Parallel processing can work on different aspects of the same program at the same time. The main purpose of parallel processing is to save time to solve large and complex problems. Concurrent processing (concurrency processing): Refers to a time period in which several programs are running to completion, and these programs are running on the same processor (CPU), but only one program at any point on the processing machine (CPU) runs

The key to concurrency is that you have the ability to handle multiple tasks, not necessarily at the same time. The key to parallelism is that you have the ability to handle multiple tasks at the same time. So, parallelism is a subset of concurrency

V, synchronous and asynchronous

computer field, synchronization means that a process executes a request, if it takes a while for the request to return information, then the process will wait until the return message is received until the execution continues; asynchronous means that the process does not have to wait, but to continue to do the following operations, Regardless of the status of other processes. The system notifies the process when a message is returned, which can improve the efficiency of execution. For example, the telephone is synchronous communication, the short interest is asynchronous communication.

Basic concepts and context switching performance loss

Java concurrent programming principle and practice three: Multi-threading and multi-process connection and the problem of resource wasting caused by context switch

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.