Concurrency vs Parallelism: Threads, Processes, async and Python's GIL Explained
The difference between concurrency and parallelism, when to use threads, processes or async/await, why Python threads don't speed up CPU-bound code, and the race conditions and deadlocks to watch for.
By Software Side Hustles Editorial Team · Published · Updated · 5 min read
Quick answer: concurrency is about dealing with many tasks at once — making progress on several by interleaving them. Parallelism is about doing many tasks at the same instant on multiple CPU cores. Waiting on networks, disks or databases (I/O-bound work) benefits from concurrency: threads or async/await. Heavy computation (CPU-bound work) needs parallelism: multiple processes, or threads in a runtime that can run them on several cores at once.
An analogy
One chef cooking three dishes — chopping while the water boils, stirring while the oven heats — is concurrent: one worker, many tasks in progress, switching during waits. Three chefs each cooking a dish is parallel. A busy restaurant kitchen is both.
A single-core machine can be concurrent but not parallel. A multi-core machine can be both.
I/O-bound vs CPU-bound
The first question for any performance problem is what the program is waiting for.
| Workload | Bottleneck | Helps | Example |
|---|---|---|---|
| I/O-bound | Network, disk, database latency | Concurrency: async, threads | Calling 100 APIs, web servers, scrapers |
| CPU-bound | Processor time | Parallelism: processes, multi-core threads | Image processing, compression, number crunching |
Making an I/O-bound task "parallel" across cores does little; making a CPU-bound task "concurrent" on one core does nothing at all.
Threads, processes and async compared
| Model | Memory | Runs in parallel? | Good for | Watch out for |
|---|---|---|---|---|
| Processes | Separate | Yes | CPU-bound work, isolation | Start-up cost; data must be serialised between them |
| Threads | Shared | Yes (except under a GIL) | I/O and CPU work in Java, C#, Go, C++ | Race conditions, deadlocks |
| async/await (event loop) | Shared, one thread | No | Many concurrent I/O operations | Blocking calls freeze everything |
| Goroutines / virtual threads | Shared | Yes | Huge numbers of concurrent tasks | Same races as threads |
Python and the GIL
CPython's Global Interpreter Lock allows only one thread to execute Python bytecode at a time. So:
- Threads do help I/O-bound code, because the GIL is released while waiting for network or disk.
- Threads do not speed up pure-Python CPU-bound code — four threads computing in a loop take about as long as one, and sometimes longer.
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
# I/O-bound: threads (or asyncio)with ThreadPoolExecutor(max_workers=10) as pool: pages = list(pool.map(fetch_url, urls))
# CPU-bound: processes, one interpreter (and one GIL) per coreif __name__ == "__main__": with ProcessPoolExecutor() as pool: results = list(pool.map(resize_image, paths))The if __name__ == "__main__": guard is required for process pools on Windows and macOS, which start workers by re-importing your module.
Two qualifications. First, libraries such as NumPy release the GIL inside their C code, so threads can run NumPy operations in parallel. Second, CPython 3.13 introduced an experimental free-threaded build without the GIL, and Python 3.14 made it officially supported (though still not the default build). Check that your dependencies support it before relying on it.
JavaScript: one thread, an event loop
JavaScript runs your code on a single thread. async/await provides concurrency for I/O: while one request waits, others proceed. But a CPU-heavy loop blocks everything — no other requests are served and the browser UI freezes. For CPU-bound work, use Worker threads in Node.js or Web Workers in the browser. See async/await mistakes for the I/O side.
Java: platform threads and virtual threads
Java threads run truly in parallel. Since Java 21, virtual threads make it cheap to have hundreds of thousands of concurrent tasks for I/O-heavy servers:
try (var executor = Executors.newVirtualThreadPerTaskExecutor()) { for (String url : urls) { executor.submit(() -> fetch(url)); }}Virtual threads help with waiting, not with computing; for CPU-bound work, a pool sized to the number of cores remains the right tool.
Race conditions
When threads share mutable data, operations that look atomic are not:
counter = 0
def work(): global counter for _ in range(100_000): counter += 1 # read, add, write — another thread can interleaveRun that in several threads and the final count may be lower than expected, depending on the interpreter version and timing — which is exactly what makes races hard to reproduce. Fixes, roughly in order of preference:
- Avoid sharing: give each worker its own data and combine results at the end (
pool.mapreturning values). - Use thread-safe structures:
queue.Queue, Java'sConcurrentHashMap,AtomicInteger, Go channels. - Lock around the critical section:
with lock:in Python,synchronizedorReentrantLockin Java.
Even single-threaded async code can race: two concurrent requests that each "read balance, then write balance minus 10" can both read the same starting value if there is an await between the read and the write. Databases solve the same problem with transactions, row locks and atomic updates (UPDATE accounts SET balance = balance - 10 WHERE id = ?).
Deadlocks
A deadlock happens when two tasks each hold a lock the other needs:
- Thread A locks
accounts[1], then waits foraccounts[2]. - Thread B locks
accounts[2], then waits foraccounts[1].
Neither can continue. Prevent it by always acquiring locks in a consistent order (for example, by ascending ID), holding locks for as little time as possible, and using timeouts.
Amdahl's law: why 8 cores is not 8× faster
If 25% of a job is inherently sequential, then even with infinite cores the maximum speed-up is 4×. Parallelising the rest helps, but the sequential part soon dominates. Measure where time actually goes before parallelising — and remember that coordination (locks, serialising data between processes) has its own cost.
Frequently asked questions
Is async faster than threads?
For many concurrent I/O operations, async uses less memory and avoids context-switch overhead. It is not faster for CPU-bound work, and it requires async-compatible libraries throughout.
Should I use multiprocessing or threading in Python?
Threading (or asyncio) for I/O-bound tasks; multiprocessing (ProcessPoolExecutor) for CPU-bound pure-Python tasks.
Is Node.js multithreaded?
Your JavaScript runs on one thread, but Node.js uses a thread pool internally for some I/O, and you can create Worker threads explicitly.
What is a thread-safe function?
One that behaves correctly when called from several threads at once, typically because it does not modify shared state or protects that state with synchronisation.
Where this skill pays off
"Use threads to speed up this CPU-bound loop" is a claim AI assistants make confidently — and in CPython it is usually wrong. Our preference labelling lab includes exactly that exercise. Spotting overclaims like this is a core skill in paid AI evaluation work; see how to prepare for technical AI evaluation work.