Share
Embed
Android Makers -- Concurrency doesn’t have to be hard: Kotlin Coroutines & Channels
Presentation of the fundamentals of Kotlin Coroutines, Channels and Flows presented at Android Makers'19
*NOTE* Several animations are not present in this slide deck; the presentation switched between live coding and slides
Jag Saund
April 24, 2019
Transcript
119 slides
-
Loaders Threads AsyncTask Callbacks Futures RxJava Functional Functional Reactive Coroutines
and Channels Communicating Sequential Processes (CSP) -
fun main() { val orders = listOf( Menu.Cappuccino(CoffeeBean.Regular, Milk.Whole), Menu.Cappuccino(CoffeeBean.Premium,
Milk.Breve), Menu.Cappuccino(CoffeeBean.Regular, Milk.NonFat), Menu.Cappuccino(CoffeeBean.Decaf, Milk.Whole), Menu.Cappuccino(CoffeeBean.Regular, Milk.NonFat), Menu.Cappuccino(CoffeeBean.Decaf, Milk.NonFat) ) } https://jagstalk.page.link/examples -
fun main() { val orders = listOf(...) orders.forEach { }
} https://jagstalk.page.link/examples -
fun main() { val orders = listOf(...) orders.forEach { val
groundBeans = grindCoffeeBeans(it.beans) } } https://jagstalk.page.link/examples -
fun main() { val orders = listOf(...) orders.forEach { val
groundBeans = grindCoffeeBeans(it.beans) val espresso = pullEspressoShot(groundBeans) } } https://jagstalk.page.link/examples -
fun main() { val orders = listOf(...) orders.forEach { val
groundBeans = grindCoffeeBeans(it.beans) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk) } } https://jagstalk.page.link/examples -
fun main() { val orders = listOf(...) orders.forEach { val
groundBeans = grindCoffeeBeans(it.beans) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk) val cappuccino = makeCappuccino(it, espresso, steamedMilk) } } https://jagstalk.page.link/examples -
fun main() { val orders = listOf(...) orders.forEach { val
groundBeans = grindCoffeeBeans(it.beans) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
fun main() { val orders = listOf(...) val time =
measureTimeMillis { orders.forEach { val groundBeans = grindCoffeeBeans(it.beans) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } log("time: $time ms") } https://jagstalk.page.link/examples -
fun main() { val orders = listOf(...) val time =
measureTimeMillis { orders.forEach { val groundBeans = grindCoffeeBeans(it.beans) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } log("time: $time ms") } https://jagstalk.page.link/examples -
fun main() {...} private fun grindCoffeeBeans(beans: CoffeeBean): CoffeeBean.GroundBeans { log("grinding
coffee beans") sleep(1000) return CoffeeBean.GroundBeans(beans) } private fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { log("pulling espresso shot") sleep(600) return Espresso(groundBeans) } private fun steamMilk(milk: Milk): Milk.SteamedMilk { log("steaming milk") sleep(300) return Milk.SteamedMilk(milk) } private fun makeCappuccino(order: Menu.Cappuccino, espresso: Espresso, steamedMilk: Milk.SteamedMilk): Beverage.Cappuccino { log("making cappuccino") sleep(100) return Beverage.Cappuccino(order, espresso, steamedMilk) } https://jagstalk.page.link/examples -
fun main() { val orders = listOf(...) val time =
measureTimeMillis { orders.forEach { val groundBeans = grindCoffeeBeans(it.beans) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } log("time: $time ms") } private fun grindCoffeeBeans(beans: CoffeeBean): CoffeeBean.GroundBeans { ... } private fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { ... } private fun steamMilk(milk: Milk): Milk.SteamedMilk { ... } private fun makeCappuccino(order: Menu.Cappuccino, espresso: Espresso, steamedMilk: Milk.SteamedMilk): Beverage.Cappuccino { ... } https://jagstalk.page.link/examples -
fun main() { val orders = listOf(...) val time =
measureTimeMillis { } log("time: $time ms") } private fun processOrders(orders: List<Menu.Cappuccino>) { } orders.forEach { val groundBeans = grindCoffeeBeans(it.beans) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } https://jagstalk.page.link/examples -
private fun processOrders(orders: List<Menu.Cappuccino>) { } fun main() { val
orders = listOf(...) val time = measureTimeMillis { processOrders(orders) } log("time: $time ms") } https://jagstalk.page.link/examples orders.forEach { val groundBeans = grindCoffeeBeans(it.beans) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } -
fun main() { val orders = listOf(...) val time =
measureTimeMillis { launch { processOrders(orders) } launch { processOrders(orders) } } log("time: $time ms”) } private fun processOrders(orders: List<Menu.Cappuccino>) { orders.forEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { launch { processOrders(orders) } launch { processOrders(orders) } } log("time: $time ms”) } private fun processOrders(orders: List<Menu.Cappuccino>) { orders.forEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { launch(CoroutineName("barista-1")) { processOrders(orders) } launch(CoroutineName("barista-2")) { processOrders(orders) } } log("time: $time ms”) } private fun processOrders(orders: List<Menu.Cappuccino>) { orders.forEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { launch(CoroutineName("barista-1")) { processOrders(orders) } launch(CoroutineName("barista-2")) { processOrders(orders) } } log("time: $time ms”) } private fun processOrders(orders: List<Menu.Cappuccino>) { orders.forEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
private fun grindCoffeeBeans(beans: CoffeeBean): CoffeeBean.GroundBeans { log("grinding coffee beans") sleep(1000)
return CoffeeBean.GroundBeans(beans) } private fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { log("pulling espresso shot") sleep(600) return Espresso(groundBeans) } private fun steamMilk(milk: Milk): Milk.SteamedMilk { log("steaming milk") sleep(300) return Milk.SteamedMilk(milk) } private fun makeCappuccino(order: Menu.Cappuccino, espresso: Espresso, steamedMilk: Milk.SteamedMilk): Beverage.Cappuccino { log("making cappuccino") sleep(100) return Beverage.Cappuccino(order, espresso, steamedMilk) } fun main() {...} https://jagstalk.page.link/examples -
private suspend fun grindCoffeeBeans(beans: CoffeeBean): CoffeeBean.GroundBeans { log("grinding coffee beans")
delay(1000) return CoffeeBean.GroundBeans(beans) } private suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { log("pulling espresso shot") delay(600) return Espresso(groundBeans) } private suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { log("steaming milk") delay(300) return Milk.SteamedMilk(milk) } private suspend fun makeCappuccino(order: Menu.Cappuccino, espresso: Espresso, steamedMilk: Milk.SteamedMilk): Beverage.Cappuccino { log("making cappuccino") delay(100) return Beverage.Cappuccino(order, espresso, steamedMilk) } fun main() {...} https://jagstalk.page.link/examples -
fun main() {...} private suspend fun grindCoffeeBeans(beans: CoffeeBean): CoffeeBean.GroundBeans {
log("grinding coffee beans") delay(1000) return CoffeeBean.GroundBeans(beans) } private suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { log("pulling espresso shot") delay(600) return Espresso(groundBeans) } private suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { log("steaming milk") delay(300) return Milk.SteamedMilk(milk) } private suspend fun makeCappuccino(order: Menu.Cappuccino, espresso: Espresso, steamedMilk: Milk.SteamedMilk): Beverage.Cappuccino { log("making cappuccino") delay(100) return Beverage.Cappuccino(order, espresso, steamedMilk) } https://jagstalk.page.link/examples -
private suspend fun grindCoffeeBeans(beans: CoffeeBean): CoffeeBean.GroundBeans { ... } private
suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { ... } private suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { ... } private suspend fun makeCappuccino(order: Menu.Cappuccino, espresso: Espresso, steamedMilk: Milk.SteamedMilk): Beverage.Cappuccino { ... } fun main() {...} https://jagstalk.page.link/examples -
private fun processOrders(orders: List<Menu.Cappuccino>) { orders.forEach { val groundBeans =
grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } private suspend fun grindCoffeeBeans(beans: CoffeeBean): CoffeeBean.GroundBeans { ... } private suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { ... } private suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { ... } private suspend fun makeCappuccino(order: Menu.Cappuccino, espresso: Espresso, steamedMilk: Milk.SteamedMilk): Beverage.Cappuccino { ... } fun main() {...} https://jagstalk.page.link/examples -
private suspend fun processOrders(orders: List<Menu.Cappuccino>) { orders.forEach { val groundBeans
= grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } private suspend fun grindCoffeeBeans(beans: CoffeeBean): CoffeeBean.GroundBeans { ... } private suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { ... } private suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { ... } private suspend fun makeCappuccino(order: Menu.Cappuccino, espresso: Espresso, steamedMilk: Milk.SteamedMilk): Beverage.Cappuccino { ... } fun main() {...} https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { launch(CoroutineName("barista-1")) { processOrders(orders) } launch(CoroutineName("barista-2")) { processOrders(orders) } } log("time: $time ms”) } private suspend fun processOrders(orders: List<Menu.Cappuccino>) { orders.forEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } private suspend fun grindCoffeeBeans(beans: CoffeeBean): CoffeeBean.GroundBeans { ... } private suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { ... } private suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { ... } private suspend fun makeCappuccino(order: Menu.Cappuccino, espresso: Espresso, steamedMilk: Milk.SteamedMilk): Beverage.Cappuccino { ... } https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(orders) } launch(CoroutineName("barista-2")) { processOrders(orders) } } } log("time: $time ms”) } private suspend fun processOrders(orders: List<Menu.Cappuccino>) { orders.forEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } private suspend fun grindCoffeeBeans(beans: CoffeeBean): CoffeeBean.GroundBeans { ... } private suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { ... } private suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { ... } private suspend fun makeCappuccino(order: Menu.Cappuccino, espresso: Espresso, steamedMilk: Milk.SteamedMilk): Beverage.Cappuccino { ... } https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(orders) } launch(CoroutineName("barista-2")) { processOrders(orders) } } } log("time: $time ms”) } val ordersChannel = Channel<Menu>() orders.forEach { ordersChannel.send(it) } https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(orders) } launch(CoroutineName("barista-2")) { processOrders(orders) } } } log("time: $time ms”) } val ordersChannel = Channel<Menu>() orders.forEach { ordersChannel.send(it) } https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms”) } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } val ordersChannel = Channel<Menu>() orders.forEach { ordersChannel.send(it) } https://jagstalk.page.link/examples -
private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans
= grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } fun main() = runBlocking { val orders = listOf(...) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms”) } val ordersChannel = Channel<Menu>() orders.forEach { ordersChannel.send(it) } https://jagstalk.page.link/examples -
val ordersChannel = Channel<Menu>() orders.forEach { ordersChannel.send(it) } fun main()
= runBlocking { val orders = listOf(...) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms”) } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
main coroutine val ordersChannel = Channel<Menu>() orders.forEach { ordersChannel.send(it) }
fun main() = runBlocking { val orders = listOf(...) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms”) } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
main coroutine channel main coroutine val ordersChannel = Channel<Menu>() orders.forEach
{ ordersChannel.send(it) } fun main() = runBlocking { val orders = listOf(...) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms”) } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
main coroutine channel val ordersChannel = Channel<Menu>() orders.forEach { ordersChannel.send(it)
} fun main() = runBlocking { val orders = listOf(...) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms”) } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
main coroutine channel val ordersChannel = Channel<Menu>() orders.forEach { ordersChannel.send(it)
} fun main() = runBlocking { val orders = listOf(...) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms”) } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
main coroutine (suspended) channel val ordersChannel = Channel<Menu>() orders.forEach {
ordersChannel.send(it) } fun main() = runBlocking { val orders = listOf(...) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms”) } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
val ordersChannel = Channel<Menu>() orders.forEach { ordersChannel.send(it) } fun main()
= runBlocking { val orders = listOf(...) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms”) } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms") } val ordersChannel = Channel<Menu.Cappuccino>() launch(CoroutineName("cashier")) { orders.forEach { ordersChannel.send(it) } } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms") } val ordersChannel = produce(CoroutineName("cashier")) { orders.forEach { send(it) } } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
fun main() = runBlocking { val orders = listOf(...) val
time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel) } } } log("time: $time ms") } val ordersChannel = produce(CoroutineName("cashier")) { orders.forEach { send(it) } } private suspend fun processOrders(ordersChannel: Channel<Menu.Cappuccino>) { ordersChannel.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = pullEspressoShot(groundBeans) val steamedMilk = steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples produce coroutine -
class EspressoMachine { suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { TODO("pull
espresso shot not implemented") } suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { TODO("steam milk not implemented") } } https://jagstalk.page.link/examples -
class EspressoMachine { suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { TODO("pull
espresso shot not implemented") } suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { TODO("steam milk not implemented") } private suspend fun processEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { log("pulling espresso shot") delay(600) return Espresso(groundBeans) } private suspend fun processSteamMilk(milk: Milk): Milk.SteamedMilk { log("steaming milk") delay(300) return Milk.SteamedMilk(milk) } } https://jagstalk.page.link/examples -
class EspressoMachine { suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { TODO("pull
espresso shot not implemented") } suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { TODO("steam milk not implemented") } } private suspend fun processEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso {...} private suspend fun processSteamMilk(milk: Milk): Milk.SteamedMilk {…} https://jagstalk.page.link/examples -
class EspressoMachine { private val portafilterOne: SendChannel<CoffeeBean.GroundBeans> = actor {
consumeEach { groundBeans -> val espresso = processEspressoShot(groundBeans) TODO("deliver espresso shot to sender") } } suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { TODO("pull espresso shot not implemented") } suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { TODO("steam milk not implemented") } } private suspend fun processEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso {...} private suspend fun processSteamMilk(milk: Milk): Milk.SteamedMilk {…} https://jagstalk.page.link/examples -
class EspressoMachine { private val portafilterOne: SendChannel<CoffeeBean.GroundBeans> = actor {
consumeEach { groundBeans -> val espresso = processEspressoShot(groundBeans) TODO("deliver espresso shot to sender") } } private val portafilterTwo: SendChannel<CoffeeBean.GroundBeans> = actor { consumeEach { groundBeans -> val espresso = processEspressoShot(groundBeans) TODO("deliver espresso shot to sender") } } suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { TODO("pull espresso shot not implemented") } suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { TODO("steam milk not implemented") } private suspend fun processEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso {...} private suspend fun processSteamMilk(milk: Milk): Milk.SteamedMilk {...} } https://jagstalk.page.link/examples -
Barista Portafilter 1 Portafilter 2 select Select Like a switch
statement Works on Send and Receive Shape Data Flow https://jagstalk.page.link/examples -
class EspressoMachine { private val portafilterOne: SendChannel<CoffeeBean.GroundBeans> = actor {
consumeEach { groundBeans -> val espresso = processEspressoShot(groundBeans) TODO("deliver espresso shot to sender") } } private val portafilterTwo: SendChannel<CoffeeBean.GroundBeans> = actor { consumeEach { groundBeans -> val espresso = processEspressoShot(groundBeans) TODO("deliver espresso shot to sender") } } suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { TODO("pull espresso shot not implemented") } suspend fun steamMilk(milk: Milk): Milk.SteamedMilk { TODO("steam milk not implemented") } private suspend fun processEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso {...} private suspend fun processSteamMilk(milk: Milk): Milk.SteamedMilk {...} } https://jagstalk.page.link/examples -
class EspressoMachine { private val portafilterOne: SendChannel<CoffeeBean.GroundBeans> = actor {...}
private val portafilterTwo: SendChannel<CoffeeBean.GroundBeans> = actor {...} suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { return select { portafilterOne.onSend(groundBeans) { TODO("sent to portafilter one - wait for result") } portafilterTwo.onSend(groundBeans) { TODO("sent to portafilter two - wait for result") } } } ... } https://jagstalk.page.link/examples -
class EspressoMachine { private val portafilterOne: SendChannel<CoffeeBean.GroundBeans> = actor {...}
private val portafilterTwo: SendChannel<CoffeeBean.GroundBeans> = actor {...} suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { return select { portafilterOne.onSend(groundBeans) { TODO("sent to portafilter one - wait for result") } portafilterTwo.onSend(groundBeans) { TODO("sent to portafilter two - wait for result") } } } ... } https://jagstalk.page.link/examples -
class EspressoMachine { private data class EspressoShotRequest( val deferredEspressoShot: CompletableDeferred<Espresso>,
val groundBeans: CoffeeBean.GroundBeans ) private val portafilterOne: SendChannel<CoffeeBean.GroundBeans> = actor {...} private val portafilterTwo: SendChannel<CoffeeBean.GroundBeans> = actor {...} suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { return select { portafilterOne.onSend(groundBeans) { TODO("sent to portafilter one - wait for result") } portafilterTwo.onSend(groundBeans) { TODO("sent to portafilter two - wait for result") } } } ... } https://jagstalk.page.link/examples -
class EspressoMachine { private data class EspressoShotRequest( val deferredEspressoShot: CompletableDeferred<Espresso>,
val groundBeans: CoffeeBean.GroundBeans ) private val portafilterOne: SendChannel<EspressoShotRequest> = actor { consumeEach { request -> val espresso = processEspressoShot(request.groundBeans) request.deferredEspressoShot.complete(espresso) } } private val portafilterTwo: SendChannel<EspressoShotRequest> = actor {...} suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { return select { portafilterOne.onSend(groundBeans) { TODO("sent to portafilter one - wait for result") } portafilterTwo.onSend(request) { TODO("sent to portafilter two - wait for result") } } } ... } https://jagstalk.page.link/examples -
class EspressoMachine { private data class EspressoShotRequest( val deferredEspressoShot: CompletableDeferred<Espresso>,
val groundBeans: CoffeeBean.GroundBeans ) private val portafilterOne: SendChannel<EspressoShotRequest> = actor { consumeEach { request -> val espresso = processEspressoShot(request.groundBeans) request.deferredEspressoShot.complete(espresso) } } private val portafilterTwo: SendChannel<EspressoShotRequest> = actor {...} suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { val request = EspressoShotRequest(CompletableDeferred(), groundBeans) return select { portafilterOne.onSend(request) { request.deferredEspressoShot.await() } portafilterTwo.onSend(request) { TODO("sent to portafilter two - wait for result") } } } ... } https://jagstalk.page.link/examples -
class EspressoMachine(scope: CoroutineScope): CoroutineScope by scope { private data class
EspressoShotRequest( val deferredEspressoShot: CompletableDeferred<Espresso>, val groundBeans: CoffeeBean.GroundBeans ) private val portafilterOne: SendChannel<EspressoShotRequest> = actor { consumeEach { request -> val espresso = processEspressoShot(request.groundBeans) request.deferredEspressoShot.complete(espresso) } } private val portafilterTwo: SendChannel<EspressoShotRequest> = actor {...} suspend fun pullEspressoShot(groundBeans: CoffeeBean.GroundBeans): Espresso { val request = EspressoShotRequest(CompletableDeferred(), groundBeans) return select { portafilterOne.onSend(request) { request.deferredEspressoShot.await() } portafilterTwo.onSend(request) { TODO("sent to portafilter two - wait for result") } } } ... } https://jagstalk.page.link/examples -
class EspressoMachine(scope: CoroutineScope): CoroutineScope by scope { ... } fun
main() = runBlocking { ... val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel, espressoMachine) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel, espressoMachine) } } } log("time: $time ms") } private suspend fun processOrders(orders: ReceiveChannel<Menu.Cappuccino>, espressoMachine: EspressoMachine) { orders.consumeEach { val groundBeans = grindCoffeeBeans(it.beans()) val espresso = espressoMachine.pullEspressoShot(groundBeans) val steamedMilk = espressoMachine.steamMilk(it.milk()) val cappuccino = makeCappuccino(it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
private suspend fun processOrders(orders: ReceiveChannel<Menu.Cappuccino>, espressoMachine: EspressoMachine) { orders.consumeEach {
val groundBeans = grindCoffeeBeans(it.beans()) val espressoDeferred = async { espressoMachine.pullEspressoShot(groundBeans) } val steamedMilkDeferred = async { espressoMachine.steamMilk(it.milk()) } val cappuccino = makeCappuccino( it, espresso, steamedMilk) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
private suspend fun processOrders(orders: ReceiveChannel<Menu.Cappuccino>, espressoMachine: EspressoMachine) { orders.consumeEach {
val groundBeans = grindCoffeeBeans(it.beans()) val espressoDeferred = async { espressoMachine.pullEspressoShot(groundBeans) } val steamedMilkDeferred = async { espressoMachine.steamMilk(it.milk()) } val cappuccino = makeCappuccino( it, espressoDeferred.await(), steamedMilkDeferred.await()) log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
private suspend fun processOrders(orders: ReceiveChannel<Menu.Cappuccino>, espressoMachine: EspressoMachine) { orders.consumeEach {
val groundBeans = grindCoffeeBeans(it.beans()) coroutineScope { val espressoDeferred = async { espressoMachine.pullEspressoShot(groundBeans) } val steamedMilkDeferred = async { espressoMachine.steamMilk(it.milk()) } val cappuccino = makeCappuccino( it, espressoDeferred.await(), steamedMilkDeferred.await()) } log("serve: $cappuccino") } } https://jagstalk.page.link/examples -
Flow Channels are for Hot Streams Flow is for Cold
Streams Flow is activated with collect https://jagstalk.page.link/examples -
https://jagstalk.page.link/examples fun main() = runBlocking { val orders = listOf(...)
val ordersChannel = produce(CoroutineName("cashier")) { orders.forEach { send(it) } } val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel, espressoMachine) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel, espressoMachine) } } } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples fun main() = runBlocking { val orders = listOf(...)
val ordersChannel = produce(CoroutineName("cashier")) { orders.forEach { send(it) } } val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel, espressoMachine) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel, espressoMachine) } } } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples fun main() = runBlocking { val orders = listOf(...)
val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel, espressoMachine) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel, espressoMachine) } } } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples fun main() = runBlocking { val orders = listOf(...)
val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel, espressoMachine) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel, espressoMachine) } } } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: ReceiveChannel<Menu.Cappuccino>, espressoMachine: EspressoMachine) { orders.consumeEach
{ val groundBeans = grindCoffeeBeans(it.beans()) coroutineScope { val espressoDeferred = async { espressoMachine.pullEspressoShot(groundBeans) } val steamedMilkDeferred = async { espressoMachine.steamMilk(it.milk()) } val cappuccino = makeCappuccino(it, espressoDeferred.await(), steamedMilkDeferred.await()) log("serve: $cappuccino") } } } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { ... } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: Flow<Menu.Cappuccino>, espressoMachine: EspressoMachine) { orders.consumeEach
{ val groundBeans = grindCoffeeBeans(it.beans()) coroutineScope { val espressoDeferred = async { espressoMachine.pullEspressoShot(groundBeans) } val steamedMilkDeferred = async { espressoMachine.steamMilk(it.milk()) } val cappuccino = makeCappuccino(it, espressoDeferred.await(), steamedMilkDeferred.await()) log("serve: $cappuccino") } } } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { ... } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: Flow<Menu.Cappuccino>, espressoMachine: EspressoMachine) { orders.consumeEach
{ val groundBeans = grindCoffeeBeans(it.beans()) coroutineScope { val espressoDeferred = async { espressoMachine.pullEspressoShot(groundBeans) } val steamedMilkDeferred = async { espressoMachine.steamMilk(it.milk()) } val cappuccino = makeCappuccino(it, espressoDeferred.await(), steamedMilkDeferred.await()) log("serve: $cappuccino") } } } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { ... } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: Flow<Menu.Cappuccino>, espressoMachine: EspressoMachine) { orders.map
{ val groundBeans = grindCoffeeBeans(it.beans()) coroutineScope { val espressoDeferred = async { espressoMachine.pullEspressoShot(groundBeans) } val steamedMilkDeferred = async { espressoMachine.steamMilk(it.milk()) } val cappuccino = makeCappuccino(it, espressoDeferred.await(), steamedMilkDeferred.await()) log("serve: $cappuccino") } } } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { ... } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: Flow<Menu.Cappuccino>, espressoMachine: EspressoMachine) { orders.map
{ val groundBeans = grindCoffeeBeans(it.beans()) coroutineScope { val espressoDeferred = async { espressoMachine.pullEspressoShot(groundBeans) } val steamedMilkDeferred = async { espressoMachine.steamMilk(it.milk()) } val cappuccino = makeCappuccino(it, espressoDeferred.await(), steamedMilkDeferred.await()) log("serve: $cappuccino") } } } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { ... } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: Flow<Menu.Cappuccino>, espressoMachine: EspressoMachine) { orders.map
{ val groundBeans = grindCoffeeBeans(it.beans()) coroutineScope { val espressoDeferred = async { espressoMachine.pullEspressoShot(groundBeans) } val steamedMilkDeferred = async { espressoMachine.steamMilk(it.milk()) } makeCappuccino(it, espressoDeferred.await(), steamedMilkDeferred.await()) } } } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { ... } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: Flow<Menu.Cappuccino>, espressoMachine: EspressoMachine): Flow<Beverage.Cappuccino> =
orders.map { val groundBeans = grindCoffeeBeans(it.beans()) coroutineScope { val espressoDeferred = async { espressoMachine.pullEspressoShot(groundBeans) } val steamedMilkDeferred = async { espressoMachine.steamMilk(it.milk()) } makeCappuccino(it, espressoDeferred.await(), steamedMilkDeferred.await()) } } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { ... } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: Flow<Menu.Cappuccino>, espressoMachine: EspressoMachine): Flow<Beverage.Cappuccino> {
... } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { coroutineScope { launch(CoroutineName("barista-1")) { processOrders(ordersChannel, espressoMachine) } launch(CoroutineName("barista-2")) { processOrders(ordersChannel, espressoMachine) } } } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: Flow<Menu.Cappuccino>, espressoMachine: EspressoMachine): Flow<Beverage.Cappuccino> {
... } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { flowOf( processOrders(ordersFlow, espressoMachine), processOrders(ordersFlow, espressoMachine) ) } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: Flow<Menu.Cappuccino>, espressoMachine: EspressoMachine): Flow<Beverage.Cappuccino> {
... } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { flowOf( processOrders(ordersFlow, espressoMachine), processOrders(ordersFlow, espressoMachine) ) .flattenMerge() } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples private suspend fun processOrders(orders: Flow<Menu.Cappuccino>, espressoMachine: EspressoMachine): Flow<Beverage.Cappuccino> {
... } fun main() = runBlocking { val orders = listOf(...) val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { flowOf( processOrders(ordersFlow, espressoMachine), processOrders(ordersFlow, espressoMachine) ) .flattenMerge() .collect { cappuccino -> log("serve: $cappuccino") } } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples fun main() = runBlocking { val orders = listOf(...)
val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() .produceIn(this) .asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { flowOf( processOrders(ordersFlow, espressoMachine), processOrders(ordersFlow, espressoMachine) ) .flattenMerge() .collect { cappuccino -> log("serve: $cappuccino") } } log("time: $time ms") espressoMachine.destroy() } -
https://jagstalk.page.link/examples fun main() = runBlocking { val orders = listOf(...)
val ordersFlow: Flow<Menu.Cappuccino> = orders.asFlow() .produceIn(this) .asFlow() val espressoMachine = EspressoMachine(this) val time = measureTimeMillis { flowOf( processOrders(ordersFlow, espressoMachine), processOrders(ordersFlow, espressoMachine) ) .flattenMerge() .collect { cappuccino -> log("serve: $cappuccino") } } log("time: $time ms") espressoMachine.destroy() } private fun <T> ReceiveChannel<T>.asFlow(): Flow<T> = flow { consumeEach { value -> emit(value) } }