July 2018
Intermediate to advanced
266 pages
7h 11m
English
Let's modify the example at the beginning of this chapter so that asyncIncrement() runs confined to a single thread:
var counter = 0val context = newSingleThreadContext("counter")fun asyncIncrement(by: Int) = async(context) { for (i in 0 until by) { counter++ }}
Now, regardless of how many times asyncIncrement() is called, it will run on a single thread, meaning that all the changes made to counter will be sequential.

Read now
Unlock full access