For the More Curious: Race Conditions
Whenever you are running code in parallel, you need to be aware of race conditions. A race condition is a scenario in which your program behaves incorrectly when instructions execute at an unexpected time or in an unexpected order.
To see a race condition in action, imagine a booking system that would like to efficiently check in its passengers and count the number of passengers it has processed. If there are 1,000 flights with 75 passengers each, you might decide to use a separate coroutine to process each flight’s 75 passengers. To implement this, you can write code that looks like this. Try it in the REPL.
(When you launch the REPL, you will have the choice of launching it in the context of ...
Get Kotlin Programming: The Big Nerd Ranch Guide, 2nd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.