Concurrency and parallelism are two similar and different concepts, but often easily confused, what is the difference between them? This article gives you a better understanding by an example (this article is translated by the Concurrent Programming network).
Modern society is parallel: multi-core, network, cloud computing, user load, concurrency technology is useful for this.
The go language supports concurrency, which provides: concurrent execution (Goroutines), synchronization and messaging (channels), and multi-channel concurrency control (select).
Differences in concurrency and parallelism
When go claims to be concurrent, people say: "Concurrency is cool!" Yes, I can run it in parallel! "But that's a mistake. Because a lot of people don't know the difference between them. "I used four processors to filter the prime numbers, but it was much slower. ”
- Concurrency (Concurrency): Programming in a process collection that can be executed independently (the process is a hard-to-name definition, this is a normal process, not a Linux process)
- Parallel (Parallelism): Programming in a way that can be executed concurrently (possibly related) to a computational instruction.
The difference between the two: concurrency is a lot of work at the same time (dealing), parallel is doing a lot of things at the same time (doing). Different, but also relevant. One is about the code structure, and one is about code execution. Concurrency provides a solution for possible (not required) parallel problems. Like what:
- The mouse, keyboard, monitor, and disk drivers are concurrency-structured.
- The vector dot product is parallel.
Concurrent with communication
Concurrency is a way of constructing programs that break down tasks into small tasks that run independently. Communication is a means of coordinating these small tasks.
The Go model (and Erlang, etc.) are based on CPS (communicating sequential processes, communication sequence process): its paper c. A. R. hoare:communicating Sequential Processes (CACM 1978)
Through an example to understand
The above is too abstract, let us give an example of the actual point.
Our problem: It takes a lot of time for a gopher to take a bunch of discarded language instructions to the stove.
More Land rats
More hamsters alone do not solve the problem, they need more carts.
More hamsters, more carts
This will speed up, but they will encounter bottlenecks in the stacks of books and stoves. Also synchronize two hamsters, which can be implemented by means of messages.
Double All
This will be transported at twice times the speed. This is the concurrent composition of two Gopher programs (concurrent composition).
But this design is not spontaneous parallel, if only one hamster at a time in the transport will be what?
This design is still concurrent, not parallel. [Translator Note: A hamster will ship a pile of books above, then the second gopher will ship the pile of books below. Only one hamster is allowed to be transported at a time, so it is not parallel. ]
However, this scenario can be spontaneous and parallel. The concurrency component can consider other models.
Another type of design
Three hamsters are at work, but there may be delays. Each gopher is a separate step, with additional coordination (communication).
Finer-grained concurrency
Add a gopher to transport go home carts. Four of rats are working and running more smoothly, and each gopher is doing a simple task.
If we make things good enough (hard but not impossible in reality), it will be four times times as fast as the one with the first gopher.
Observation: We added a concurrent step (fourth hamster) to an existing design (the one designed for three hamsters) to enhance the performance of the system. More hamsters do more work and the system works better. Concurrency has deeper insight into problems than simple parallelism.
We have four concurrent steps: 1. Put the book on the trolley. 2. Bring the cart to the Stove 3. Unload the book into the stove. 4. Transport Go Home trolley
Different concurrent designs can be parallel in different ways.
More parallel
We are parallel in another dimension, and it is easy to exercise such a design. Eight hamsters, all in busy working.
But it may not be parallel at all.
Remember: Even if only one gopher is working at a time (0 parallel), this is a good concurrency solution.
Another type of design
The following is also a design that solves the problem with concurrency components. Two gopher, plus a relay heap.
Parallel in a general way
Increase throughput with more concurrent programs
or a different way.
Introducing a transfer heap in a multi-rat concurrency model
Fully optimized
With all of our technology, 16 rats are all in full throttle.
Learn the Content
We have a lot of ways to decompose the problem, which is the concurrent design. As soon as we break down the problem, parallelism naturally arises, and correctness becomes easy.
Back to Calculation
Our questions about the book of operations can be seen as the following analogy: The book heap is the Web data, the hamster is the CPU, the cart is scheduling, rendering or network, the fire is agent, browser or other consumers. The Gopher provides network data, which is the concurrent design of a scalable Web service.
Java multi-Threading vs. Multi-process