Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Grokking Coroutines (MinneBar)

Daniel Lew
October 13, 2020

Grokking Coroutines (MinneBar)

What are coroutines, really?

Given at MinneBar 2020.

Daniel Lew

October 13, 2020

Transcript

39 slides
  1. myScope.launch { val image = downloadImage() displayImage(image) } suspend fun

    downloadImage() = withContext(Dispatchers.IO) { … } fun displayImage(image: Image)
  2. sequence { var cur = 1 var next = 1

    while (true) { yield(cur) val tmp = cur + next cur = next next = tmp } }
  3. Subroutines fun sumSquaredValues(values: List<Int>): Int { return values.sumBy { value

    -> square(value) } } fun square(value: Int): Int = value * value
  4. val iterator = sequence.iterator() while (iterator.hasNext()) { val next =

    iterator.next() println(next) } sequence { var cur = 1 var next = 1 while (true) { yield(cur) val tmp = cur + next cur = next next = tmp } } Consumer Producer
  5. val iterator = sequence.iterator() while (iterator.hasNext()) { val next =

    iterator.next() println(next) } sequence { var cur = 1 var next = 1 while (true) { yield(cur) val tmp = cur + next cur = next next = tmp } } Consumer Producer
  6. class Fibonacci { var cur = 1 var next =

    1 fun next(): Int { val toReturn = cur val tmp = cur + next cur = next next = tmp return toReturn } } sequence { var cur = 1 var next = 1 while (true) { yield(cur) val tmp = cur + next cur = next next = tmp } } Versus
  7. Multiple Suspend Points sequence { yield(1) // first Fibonacci number

    var cur = 1 var next = 1 while (true) { yield(next) // next Fibonacci number val tmp = cur + next cur = next next = tmp } }
  8. Why use coroutines? • Another tool for writing good code

    • Two ways to use tool: • Code Structure • Concurrency
  9. Two Tasks, One Thread suspend fun task(name: String, delay: Long)

    { joinAll( async { doSomething("First", 10) }, async { doSomething("Second", 5) } ) } suspend fun doSomething(name: String, delay: Long) { println("$name START (${Thread.currentThread().name})") delay(delay) println("$name END (${Thread.currentThread().name})") } First START (main) Second START (main) Second END (main) First END (main)
  10. Callback Hell requestRandomUrl { url -> downloadImage(url) { image ->

    displayImage(image) } } myScope.launch { val url = requestRandomUrl() val image = downloadImage(url) displayImage(image) } Vs
  11. Why use coroutines? • Concurrency • Coroutines *are* like light-weight

    threads! • …In any language! • Code Structure • Coroutines enable simpler code
  12. Start Suspending Functions suspend fun mySuspendingFunction() fun <T> (suspend ()

    -> T).startCoroutine(completion: Continuation<T>) fun main() { ::mySuspendingFunction.startCoroutine( Continuation(EmptyCoroutineContext) { } ) }
  13. myScope.launch { val image = downloadImage() displayImage(image) } suspend fun

    downloadImage() = withContext(Dispatchers.IO) { … } fun displayImage(image: Image)
  14. myScope.launch { val image = downloadImage() displayImage(image) } suspend fun

    downloadImage() = withContext(Dispatchers.IO) { … } fun displayImage(image: Image)
  15. myScope.launch { val image = downloadImage() displayImage(image) } suspend fun

    downloadImage() = withContext(Dispatchers.IO) { … } fun displayImage(image: Image)
  16. myScope.launch { val image = downloadImage() displayImage(image) } suspend fun

    downloadImage() = withContext(Dispatchers.IO) { … } fun displayImage(image: Image)
  17. Review • Coroutines are suspending functions • Suspending functions are

    useful for concurrency • Kotlin stdlib provides coroutine support • Kotlin coroutine library adds practical functions for coroutines
  18. More Info • Longer Talk: https://youtu.be/Axq8LdQqQGQ • Links: github.com/Kotlin/kotlinx.coroutines/#documentation •

    Roman Elizarov: medium.com/@elizarov • Android + Coroutines: medium.com/@objcode • Structured concurrency: vorpus.org/blog/notes-on-structured- concurrency-or-go-statement-considered-harmful/

More Decks by Daniel Lew

Other Decks in Programming