Message passing between threads
Concurrency is a powerful feature that enables the writing of new kinds of applications. However, the execution and debugging of concurrent programs are difficult because their execution is non-deterministic. We saw this through examples in the previous section where the order of print statements varied for each run of the program. The order in which the threads will be executed is not known ahead of time. A concurrent program developer must make sure that the program will execute correctly overall, regardless of the order in which the individual threads are executed.
One way to ensure program correctness in the face of the unpredictable ordering of thread execution is to introduce mechanisms for synchronizing activities across threads. One such model for concurrent programming is message-passing concurrency. It is a way to structure the components of a concurrent program. In our case, concurrent components are threads (but they can also be processes...