Concurrency
- The composition of independently executing tasks.
- Applied when dealing with handling lots of things at once.
- The focus is on how to structure a solution to solve a problem which may or may not be solved in a parallel manner.
Concurrencyis a way to structure a program by breaking it into pieces that can be executed independently.- Communication is the means to coordinate the independent executions.
- Go supports concurrency. Go provides:
- concurrent execution (
goroutines). - synchronization and messaging (
channels). - multi-way concurrent control (
select).
- concurrent execution (
- IMPORTANT POINTS:
Concurrencyis powerful.Concurrencyis notparallelism.Concurrencyenablesparallelism.Concurrencymakesparallelism(and scaling and everything else) easy.
Parallelism
Parallelismis the simultaneous execution of computations.- Programming as the simultaneous execution of (possibly related) computations.
- It’s all about doing lots of things at once.
Concurrency vs. parallelism
- “Concurrency is about dealing with lots of things at once. Parallelism is about doing lots of things at once.” — Rob Pike
- Concurrency is a property of a program where two or more tasks can be in progress simultaneously. Parallelism is a run-time property where two or more tasks are being executed simultaneously. Through concurrency you want to define a proper structure to your program. Concurrency can use parallelism for getting its job done but remember parallelism is not the ultimate goal of concurrency.
Concurrencyis about dealing with lots of things at once.Parallelismis about doing lots of things at once.- Not the same, but related.
Concurrencyis aboutstructure,parallelismis aboutexecution.Concurrencyprovides a way tostructure a solutionto solve a problem that may (but not necessarily) beparallelizable.- An analogy
Concurrent: Mouse, keyboard, display, and disk drivers.Parallel: Vector dot product.
Source https://github.com/NanXiao/golang-101-hacks