The Execution Model: How Programs Actually Run
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.
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.