Concurrency
Concurrency is the ability of a program to make progress on multiple tasks during overlapping time periods, whether by truly running them simultaneously or by interleaving their execution on a single processor.
Definition
Concurrency is the ability of a program to make progress on multiple tasks during overlapping time periods, whether by truly running them simultaneously or by interleaving their execution on a single processor.
Overview
Concurrency is often confused with parallelism, but the two describe different things: concurrency is about structuring a program to deal with multiple tasks at once — managing their state and progress independently — while parallelism specifically means executing multiple tasks at the exact same instant on separate hardware. A single-core CPU can still run a concurrent program by rapidly switching between tasks, giving the appearance of simultaneous progress even though only one instruction executes at any given moment. Programs achieve concurrency through several distinct models. Multithreading uses OS-level threads within a process to run multiple sequences of instructions, requiring synchronization primitives to safely share memory. Asynchronous programming, by contrast, achieves concurrency on a single thread by using an event loop to interleave non-blocking operations — this is the model behind Node.js and modern JavaScript, where I/O-bound work like network requests doesn't block other code from running while it waits. Some languages, like Go, offer lightweight concurrency primitives (goroutines and channels) that sit somewhere between raw threads and pure async models. The central challenge in any concurrent system is coordinating access to shared resources without introducing race conditions, deadlocks, or other timing-dependent bugs, which is why concurrent programs are generally harder to reason about, test, and debug than strictly sequential ones. Understanding concurrency is essential for building responsive applications — UIs that don't freeze while waiting on network calls, and servers that can handle many simultaneous client connections efficiently. The famous formulation from Rob Pike, co-creator of Go, captures the distinction well: 'concurrency is about dealing with lots of things at once; parallelism is about doing lots of things at once' — concurrency is a way of structuring code, while parallelism is about execution.
Key Concepts
- Concerned with managing progress on multiple tasks, not necessarily simultaneously
- Achievable on a single core via interleaving, unlike true parallelism
- Implemented via multithreading, async/event-loop models, or lightweight coroutines
- Distinct from but often paired with parallelism on multi-core hardware
- Requires careful coordination to avoid race conditions and deadlocks
- Essential for responsive UIs and high-throughput servers
- Popularized distinction: concurrency is structure, parallelism is execution (Rob Pike)