← Home
🏗Crack System Design with DSA AnimatorSystem DesignNEW
🏗 System Design›Concurrency & Threading

🧠Concurrency Basics

Multiple tasks progress together; correctness depends on coordination.

📌 Key IdeaConcurrency means many tasks are in progress during the same time window. Parallelism means tasks are executing at the same instant on multiple cores. In interviews, explain both the performance gain and the correctness risks (race/visibility).
🖼

Visual

🧠 Concurrency (interleaving)
Many tasks are in progress in the same time window.
Core timeline:

time ──▶
Task A: [run] [wait I/O] [run]
Task B:      [run] [wait]   [run]

Single core can still be concurrent
because tasks interleave.
⚙️ Parallelism (simultaneous)
Work is literally executing at the same instant.
time t0:
Core 1 -> Task A running
Core 2 -> Task B running

Requires multiple cores/workers.
Usually used to speed up CPU-heavy work.
🎯 Interview one-liner
Concurrency = structure your program to handle many tasks.
Parallelism = runtime executes tasks at the same instant.
✅

When to Use

  • Handling many I/O-heavy requests where waiting dominates CPU time.
  • Background jobs can run independently from the request thread.
  • Pipeline stages can overlap (read, transform, write).
🚫

When NOT to Use

  • Tiny CPU-bound workloads where thread overhead dominates.
  • When correctness relies on global mutable state without clear locking.
  • Single-user scripts where sequential flow is simpler and enough.
🚨

Anti-Pattern (What NOT to Do)

Data race on shared counterBadjava
class Metrics {
    private int requests = 0;

    void markRequest() { requests++; } // not atomic
    int total() { return requests; }
}
Two threads can read same value and both write back, losing increments.
✅

The Right Way

Atomic update for shared stateGoodjava
import java.util.concurrent.atomic.AtomicInteger;

class Metrics {
    private final AtomicInteger requests = new AtomicInteger();

    void markRequest() { requests.incrementAndGet(); }
    int total() { return requests.get(); }
}
Atomic classes avoid explicit locks for simple counters.
📊

Concurrency vs Parallelism

AspectConcurrencyParallelism
MeaningMany tasks in progressMany tasks executing simultaneously
Requires multiple cores?Not necessarilyYes (for true parallel execution)
Main benefitResponsiveness and overlap of waitsRaw throughput for CPU-heavy work
Typical exampleWeb server handling many requests with thread poolImage processing split across cores
Still needs synchronization?Yes, when state is sharedYes, and bugs can appear faster
🔀

Common Confusions Cleared Up

Concurrency is a design model; parallelism is a hardware/runtime execution detail

You can write concurrent code on one core (tasks interleave). Parallelism is when two or more tasks truly run at the same time.

🎤

Interview Q&A

🧾

Quick Cheat Sheet

GoalHigher throughput and lower latency under load.
Core riskRaces, deadlocks, visibility bugs.
Primary toolsAtomic types, locks, executors, queues.
RuleShare less state; synchronize what remains.
🔗

Related Topics