Concurrency vs parallelism
Two tasks, one core, then two cores. A timeline shows the difference between juggling several tasks and truly running them at the same instant, and which kind of work each one speeds up.
In 30 seconds
Concurrency is dealing with many tasks over the same period: a single core runs a bit of one, then a bit of another, so all of them make progress.
Parallelism is doing many things at the same instant, which takes more than one core.
Tasks that mostly wait for a network or a disk (I/O-bound) gain from concurrency alone, because the core can work on something else while they wait. Tasks that compute the whole time (CPU-bound) only finish sooner with more cores.
Concurrency vs parallelism, step by step
Time units are illustrative. Real cores switch tasks many times a second, each switch has a small cost, and extra cores help only as far as the work can be split into independent parts.
- Step 1: Two tasks, one at a time. Start with two tasks that each need 4 units of computing, and one processor core to run them. The simplest plan is to finish A, then start B. It works, but B makes no progress at all until time 4.
- Step 2: Concurrency: take turns. Concurrency means dealing with several tasks over the same period. The core runs a slice of A, switches to B, and back again. Both tasks move forward, yet at any instant only one is running, so the total is still 8. Each switch even costs a little.
- Step 3: Parallelism: two cores. Parallelism means doing several things at the very same instant, and that needs more than one core. Give A and B a core each and they run side by side. The same 8 units of work are finished at time 4.
- Step 4: I/O-bound: mostly waiting. Many tasks spend most of their time waiting for input or output (I/O): a network reply, a disk, a user. They are called I/O-bound. While A waits, the core is free, so it can run B. On one core, taking turns now finishes at 5 instead of 8.
- Step 5: CPU-bound vs I/O-bound. Put the finishing times side by side. For CPU-bound work, which computes the whole time, taking turns on one core gains nothing: only a second core helps. For I/O-bound work, taking turns already recovers most of the lost time without any extra core.
- Step 6: Both together. Real programs mix the two. Here four tasks of 2 units each share two cores. Each core takes turns between two tasks, which is concurrency, and the two cores run at the same instant, which is parallelism. Concurrency is how the work is organised; parallelism is how it is executed.
- Step 7: Which one do you need?. Ask what the tasks spend their time on. A web server juggling thousands of slow connections is I/O-bound: it needs concurrency, and one core goes a long way. Resizing a thousand photos is CPU-bound: it needs parallelism, so it needs more cores.
Try it yourself
Learn this first
Learn next
- Goroutines and channelsLightweight Go functions that run side by side and pass values through channels. Intermediate
- Race conditionsTwo threads update one counter and an update vanishes, unless a lock steps in. Intermediate
- CoroutinesFunctions that pause and resume, so many tasks can take turns on one thread. Intermediate
Spotted a mistake on this page?