Concurrency and parallelism

Posted Jun 5, 20204 min read


​The operation of the software depends on the hardware infrastructure. Programming technology is also updated due to the continuous development of hardware. By understanding the development of computer hardware, you can know the development direction of the design goals of some programming languages.

Over the past half century, Moore’s Lawhas guided the development of the semiconductor and technology industries. Intel has always followed Moore's Law and generally releases new results every two years or so. However, some experts now point out thatthe computer has reached the physical limit of Moore's Law**.

As technology continues to advance, transistor production continues to shrink, but any substance has physical limits. When a single object is close to the physical limit, multiple parallel operations are generally selected to achieve the purpose of improving efficiency.

Moore's Law

Moore’s Law:Moore’s Law was created by Intel co-founder Gordon Moore in 1965. He pointed out that the number of transistors on a microchip doubles approximately every two years, and chip computing performance will also double .

Concurrency and parallelism

Concurrent Parallel is often used to describe programs and programming. For example, a system has concurrency, a programming language can support parallel computing, and so on. They are attributes of the system.

A system is said to be concurrent if it can support two or more actions in progress at the same time. A system is said to be parallel if it can support two or more actions executing simultaneously.

The Art of Concurrency

Borrow Concurrency Art Definition:"_If a system can support two or more operations in progress at the same time, it is said that the system is concurrent. If the system can support two or more operations that are performed at the same time, it is said This system is a parallel system."

For example, modern operating systems can support multiple applications at the same time in progress, indicating that it has concurrency. To achieve multiple operations simultaneous execution, the hardware support of a multi-core CPU is required.

The implementation of system concurrency in different hardware environments is different.

Single-core single-CPU, the system supports the execution of multiple tasks through task switching.

Single core single CPU

Multiple CPUs can implement parallel operations, allowing tasks to be executed at the same physical moment**.

Multiple CPU

To understand it simply, parallel can be considered as one of the ways to implement system concurrency.

Parallel Computing

Whether it is multi-core programming, distributed programming, microservices are actually elaborating on one thing, that is parallel computing. Parallel computing is to increase the processing elements to shorten the running time and improve the operating efficiency. For example, by increasing the CPU core to enhance computing power. In distributed mode, increase the throughput by adding servers and so on.

Parallel computing efficiency has a corresponding guiding theory Amdall's law.

Amdahl’s Law:Amdahl’s Law is commonly used in parallel computing to predict the theoretical speed when multiple processors are used.

For example, if a program uses a single thread, it takes 20 hours to complete. Now parallel processing, but one hour part of the program cannot be parallelized, so the execution time of the remaining 19 hours(p = 0.95) can be parallelized, no matter how many threads are added for parallel execution of the program, the shortest execution time No less than an hour. Therefore, the theoretical parallel acceleration is up to 20 times the performance of a single thread.
Amdahl's Law

Concurrency model

We first need to know about memory sharing.

Memory sharing is an effective way for computers to pass data between processes. Note that there are contention problems during asynchronous concurrent access. You can use mechanisms such as lock, semaphores and monitors to avoid this problem.

Memory Sharing Communication

In the design of concurrent models, it is necessary to distinguish between component and thread design based on shared state or independent state. The state is usually some data, and objects. State sharing may have contention issues.

In the independent state, there will be no concurrent contention problems. They communicate by exchanging immutable objects and sending copies of objects(or data) to each other.


Parallel Worker Model

This model is easier to understand. Boss accepts the request and distributes it to the workers for processing, and the workers process the entire request. java.util.concurrent Many designs are based on this model.

The advantages of this model, the operation is relatively simple, only need to increase or decrease workers according to the actual situation of the business. For example, in a crawler application, set the number of different number of workers and observe the shortest time in that case to determine the optimal number of workers.

The shortcomings of the model, the problem will become complicated in the shared state, we have to consider the contention of threads, deadlocks and other issues. In addition, the order of processing tasks is not fixed, and it is not suitable for scenarios where the order of execution is required.

Parallel Workers

Pipeline model

Pipeline model, as the name suggests, this model handles tasks like assembly line processing of pipeline workers. This model has no shared state.

Systems of this model are usually designed to use non-blocking IO. For example, when reading files from the network, the file download will not wait, and will be handed over to the next task after completion.

File Download

This concurrency model system is also called a reactive system(Reactive) or an event-driven system. Vertx is the best example of this model.

[][ )


Actors model

In the Actor model, each worker is called Actor. Actors can send messages to each other directly. Messages are sent and processed asynchronously.

Some advantages of this model are that there is no shared state.