Skip to main content

Command Palette

Search for a command to run...

Golang Concurrency #1 - Concurrency vs Parallelism

Published
1 min read
Golang Concurrency #1 - Concurrency vs Parallelism
T

Just a guy who loves to write code and watch anime.

Concurrency = dealing with multiple things at once Parallelism = doing multiple things at once

Concurrency is about structure, which is how you organize your program to handle multiple tasks. It's like a juggler keeping multiple balls in the air. The juggler doesn't throw all the balls at once but manages them in a way that looks simultaneous.

Parallelism is about execution, running multiple tasks at the same time. It's like having multiple jugglers, each throwing their own balls.

Real examples:

Concurrency without parallelism:

  • Single-core CPU running multiple programs

  • One waiter serving multiple restaurant tables

  • Go program with 1000 goroutines on a single CPU core

Parallelism:

  • Multi-core CPU where each core runs different tasks

  • Multiple waiters each serving different tables

  • Go program with goroutines spread across multiple CPU cores

Key insight: You can have concurrency without parallelism, but you can't have parallelism without concurrency.

In Go:

  • Goroutines provide concurrency

  • Go runtime decides if/how to use parallelism across CPU cores

  • You write concurrent code, Go handles the parallel execution

Think of it this way: concurrency is about handling multiple tasks, and parallelism is about performing multiple tasks. Go allows you to write concurrent programs that the runtime can execute in parallel when it is beneficial.