Racing and Unbiased Select Design Patterns
Explore Kotlin concurrency design patterns by learning the Racing pattern, which picks the fastest response among multiple tasks, and the Unbiased Select pattern, which randomly selects among simultaneous events. Understand how to implement these using select expressions to build more efficient and responsive Kotlin applications.
We'll cover the following...
Racing design pattern
The Racing design pattern runs multiple jobs concurrently, picking the result that returns first as the winner and discarding others as losers.
We can implement Racing in Kotlin using the select() function on channels.
Let’s imagine we are building a weather application. For redundancy, we fetch the weather from two different sources, Precise Weather and Weather Today. We’ll describe them as two producers that return their name and temperature.
If we have more than one ...