Concurrency FundamentalsThreads vs ProcessesEasy⏱️ ~2 min

The Execution Model: How Programs Actually Run

Key Insight
In a sequential program, line 1 runs before line 2. In a concurrent program, you cannot tell which runs first - and the answer may change every time.

The Simple World

When you write a normal program, things happen in order. Line 1 runs, then line 2, then line 3. You can trace through the code and know exactly what happens.

Two Things Break This

Multiple processors: Modern computers have 4, 8, even 64 CPUs. If two pieces of code run on different CPUs, they run at the same time. Which one finishes first? Depends on the day.

Time slicing: Even with one CPU, the operating system can run multiple threads by switching between them rapidly. Thread A runs for 10 milliseconds, then Thread B, then back to A. The scheduler decides when to switch. You have no say.

Execution Timeline Comparison
SequentialStep 1Step 2Step 3DonePredictableConcurrentA1B1B2A2A3B3?RandomThread A (blue) and Thread B (orange) interleave unpredictably each run

Think Of It Like Two Chefs

Imagine two chefs in a kitchen, both reaching for the salt. Who grabs it first? Depends on who moves faster. Run the same scenario tomorrow and the other chef might win. There is no guaranteed order.

Why This Is Hard

A concurrent bug might hide for months. Your code runs fine 10,000 times. Then on run 10,001, the scheduler makes a different decision, and everything breaks. Testing cannot catch these bugs reliably. Only careful design can prevent them.

💡 Key Takeaways
Sequential programs are predictable: line 1 before line 2, always
Multiple CPUs or time-sliced threads break this predictability
The scheduler decides when threads run - you have no control
Concurrent bugs can hide for thousands of runs before appearing
Testing cannot reliably catch concurrency bugs - only careful design helps
📌 Examples
1Two chefs reaching for salt: who grabs it first depends on timing, not code order
2Print statements: Thread A prints 'hello', Thread B prints 'world' - output order varies
3Counter increment: two threads add 1, but final result is sometimes 1 instead of 2
← Back to Threads vs Processes Overview