Codele / Advanced programming
Doing two things at once
A kitchen with one cook does one dish at a time. Programs have cooks too, called workers, and with more than one worker, or smart turn-taking, jobs finish sooner.
- A worker does the work, a task is a job to do, and a scheduler decides which task gets a worker at each moment. This lesson simulates all three with a loop of ticks.
- A thread is one line of work that the computer can run alongside others. Concurrency means tasks overlap in time (taking turns). Parallelism means they truly run at the same moment on different workers.
- Waiting (for a download, a file, a click) does not need a worker. That is why taking turns can save time even with only one worker.
- Real threads: Python threading.Thread, Java new Thread, C# new Thread or Task.Run, C++ std::thread, Go goroutines (go f()). JavaScript in a browser uses Web Workers. The simulation here prints the same everywhere, which real threads do not guarantee.
Loading the playable lesson…