A threading model describes how execution is divided into timelines and whether actions can run concurrently or interleave. JavaScript’s single main thread prevents synchronous actions from running simultaneously, but asynchronous callbacks reintroduce timing uncertainty. section 15.14 section 15.32
Use the model to reason about execution:
- Treat synchronous JavaScript actions as one uninterrupted timeline; no other synchronous action interleaves while it runs. section 15.14
- Mark asynchronous calls as future runtime activity, because callback timing can stretch the gaps between actions. section 15.14
- Simplify timeline diagrams by consolidating actions on one timeline and combining timelines that end by creating a new timeline. section 15.18 section 15.20
Shared updates versus JavaScript execution
In Java, x += y can interleave as read, add, and store across threads, producing different results. In JavaScript, synchronous reads and writes cannot interleave, while an asynchronous callback may run later and affect ordering. section 15.14
int x = 0;
public void addToX(int y) {
x += y;
}
A compact shared-variable example makes the three-step interleaving problem concrete. section 15.14
