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

Communication Break Down

Bob Dahlberg
February 02, 2020

Communication Break Down

Coroutines are great, I think we all agree on that. But as the async, and possibly parallel, programming becomes easier the risk of sharing mutable variables between coroutines arises.

When the boundaries are abstracted away we should rely on safe ways to communicate between our coroutines.

In this session, I will go through safe and unsafe ways of communication between different coroutines, and why it's not safe to think of them purely as threads.

Bob Dahlberg

February 02, 2020
Tweet

More Decks by Bob Dahlberg

Other Decks in Programming

Transcript

  1. Lightweight threads Coroutines “Think of them as lightweight threads” What

    do we mean by lightweight? Should we treat them as threads? Because they might be.
  2. Lightweight threads Coroutines “Think of them as lightweight threads” What

    do we mean by lightweight? Should we treat them as threads? Because they might be. fun main() = runBlocking<Unit> { repeat(100_000) { launch { !// creates a coroutine println(“On thread !-> $thread”) } } }
  3. Lightweight threads Coroutines “Think of them as lightweight threads” What

    do we mean by lightweight? Should we treat them as threads? Because they might be. repeat(100_000) { launch { !// creates a coroutine println(“On thread !-> $thread”) } }
  4. Lightweight threads Coroutines “Think of them as lightweight threads” What

    do we mean by lightweight? Should we treat them as threads? Because they might be. repeat(100_000) { thread { !// creates a thread println(“On thread !-> $thread”) } }
  5. Lightweight threads Coroutines “Think of them as lightweight threads” What

    do we mean by lightweight? Should we treat them as threads? Because they might be. repeat(100_000) { launch(Dispatchers.Default) { !// 8 threads println(“On thread !-> $thread”) } }
  6. Lightweight threads Coroutines “Think of them as lightweight threads” What

    do we mean by lightweight? Should we treat them as threads? Because they might be. repeat(100_000) { launch(Dispatchers.IO) { !// 84 threads println(“On thread !-> $thread”) } }
  7. Lightweight threads Coroutines How about thread safety? var i =

    0 repeat(100_000) { launch(Dispatchers.Default) { i += it } } println("Result !-> $i”)
  8. Lightweight threads Coroutines How about thread safety? var i =

    0 repeat(100_000) { launch(Dispatchers.IO) { i += it } } println("Result !-> $i”)
  9. Lightweight threads Coroutines How about thread safety? var i =

    0 repeat(100_000) { launch { i += it } } println("Result !-> $i”)
  10. Lightweight threads Coroutines How about thread safety? var i =

    0 launch(Dispatchers.Default) { repeat(100_000) { launch { i += it } } println("Result !-> $i”) }
  11. Lightweight threads Coroutines How about thread safety?
 @Volatile var i

    = 0 launch(Dispatchers.Default) { repeat(100_000) { launch { i += it } } println("Result !-> $i”) }
  12. Treat them as threads? Coroutines So treat them as threads

    and we are fine? launch(Dispatchers.Unconfined) { println(“A1. On thread $thread”) delay(200) println(“A2. On thread $thread”) } A1. On thread main A2. On thread worker-1
  13. Treat them as threads? Coroutines fun main() = runBlocking<Unit>{ launch(Dispatchers.IO)

    { println(“A1. On thread $thread”) switchContext() println(“A2. On thread $thread”) } } suspend fun switchContext() { withContext(Dispatchers.Default) { println(“B1. Switching $thread”) } } A1. On thread worker-1 B1. Switching worker-1 A2. On thread worker-1 A1. On thread worker-1 B1. Switching worker-3 A2. On thread worker-3
  14. Treat them as threads? Coroutines val local = ThreadLocal<String?>() fun

    main() = runBlocking<Unit>{ launch(Dispatchers.IO) { local.set("IO") println(“A1. ${local.get()}”) switchContext() println(“A2. ${local.get()}”) } } suspend fun switchContext() { withContext(Dispatchers.Default) { println(“B1. ${local.get()}”) local.set("Default") } } A1. IO B1. IO A2. Default A1. IO B1. null A2. Default
  15. Treat them as coroutines! Coroutines So treat them as threads

    and we are fine? Nope, treat them as coroutines!
  16. Treat them as coroutines! Coroutines Excellent example from Dan Lew

    (blog.danlew.net)
 @Synchronized fun criticalSection() { println(“Starting!") Thread.sleep(10) println("Ending!") } repeat(2) { thread { criticalSection() } } Starting! Ending! Starting! Ending!
  17. Treat them as coroutines! Coroutines Excellent example from Dan Lew

    (blog.danlew.net)
 @Synchronized suspend fun criticalSection() { println("Starting!") delay(10) println("Ending!") } repeat(2) { launch(Dispatchers.Default) { criticalSection() } } Starting! Starting! Ending! Ending!
  18. Treat them as coroutines! Coroutines Excellent example from Dan Lew

    (blog.danlew.net)
 @Synchronized fun criticalSection() { println("Starting!") Thread.sleep(10) println("Ending!") } repeat(2) { launch(Dispatchers.Default) { criticalSection() } }
  19. Deferred Communication val result = async { delay(2000) } val

    result2 = async { delay(1000) } println(“Deferred !-> ${result.await()}”) println(“Deferred !-> ${result2.await()}”)
  20. Deferred Communication data class Temp(var name: String) val result =

    CompletableDeferred<Temp>() launch(Dispatchers.Default) { val temp = Temp("Bob") result.complete(temp) temp.name = "Charlie" } val temp = result.await() println("Deferred !-> $temp") Deferred !-> Temp(name=“Bob”) or Deferred !-> Temp(name=“Charlie”)
  21. Deferred Communication data class Temp(val name: String) val result =

    CompletableDeferred<Temp>() launch(Dispatchers.Default) { result.complete(Temp("Bob")) delay(1) result.complete(Temp("Charlie")) } val temp = result.await() println("Deferred !-> $temp”) Deferred !-> Temp(name=“Bob”)
  22. Deferred Communication data class Temp(var name: String) val result =

    CompletableDeferred<Temp>() launch(Dispatchers.Default) { result.complete(Temp("Bob")) delay(1) result.complete(Temp("Charlie")) } launch(Dispatchers.IO) { val temp = result.await() println("Deferred !-> $temp”) }
  23. Channels Communication val channel = Channel<String>() launch(Dispatchers.Default) { channel.send("Bob") channel.send("Charlie")

    } println(“Get !-> ${channel.receive()}”) println(“Get !-> ${channel.receive()}”)
  24. Channels Communication suspend fun race(name:String): String { delay(nextLong(5000)) return name

    } val ch = Channel<String>() launch(…) { ch.send(race("Bob")) } launch(…) { ch.send(race(“Charlie")) } launch(Dispatchers.Default) { repeat(2) { println(“Name: ${ch.receive()}") } ch.close() } Name: Charlie | Name: Bob Name: Bob | Name: Charlie
  25. Channels Communication val ch = Channel<Int>() launch(Dispatchers.Default) { repeat(30) {

    ch.send(it) } ch.close() } repeat(3) { id !-> launch(Dispatchers.Default) { for(msg in ch) { println(“$id !-> $msg”) } } } 0 !-> 0 2 !-> 1 1 !-> 2 ……
  26. Channels Communication val ch = produce { repeat(30) { send(it)

    } } repeat(3) { id !-> launch(Dispatchers.Default) { ch.consumeEach { println("$id !-> $it") } } } 0 !-> 0 2 !-> 1 1 !-> 2 ……
  27. Mutex Communication val mutex = Mutex() var i = 0

    repeat(100_000) { launch(Dispatchers.Default) { mutex.withLock { i += it } } println("Result !-> $i”)
  28. Mutex Communication val mutex = Mutex() suspend fun criticalSection() {

    mutex.withLock { println("Starting!") delay(10) println("Ending!") } } repeat(2) { launch { criticalSection() } }
  29. Flow Communication val example: Flow<Int> = flow { for(i in

    1!..10) { emit(i) } } example.collect { println("Value !-> $it") } Value !-> 1 Value !-> 2 … Value !-> 10
  30. Flow Communication val example = flow { for(i in 1!..10)

    { emit(i) } } example.filter { it !>= 5 } .map { it * 2 } .collect { println("Value !-> $it") } Value !-> 10 Value !-> 12 … Value !-> 20
  31. Flow Communication val example = (1!..10).asFlow() example.filter { it !>=

    5 } .map { it * 2 } .collect { println("Value !-> $it") } Value !-> 10 Value !-> 12 … Value !-> 20
  32. Flow Communication val example = flow { for(i in 1!..10)

    { println(“Flow on !-> ${thread()}”) emit(i) } } example.filter { it !>= 5 } .map { it * 2 } .collect { println(“Collect on !-> ${thread()}”) } Flow on !-> main Collect on !-> main
  33. Flow Communication val example = flow { for(i in 1!..10)

    { println(“Flow on !-> ${thread()}”) emit(i) } }.flowOn(Dispatchers.Default) example.filter { it !>= 5 } .map { it * 2 } .collect { println(“Collect on !-> ${thread()}”) } Flow on !-> worker-1 Collect on !-> main
  34. Flow Communication val example = flow { for(i in 1!..10)

    { println(“Flow on !-> ${thread()}”) emit(i) } }.flowOn(Dispatchers.Default) example.filter { it !>= 5 } .map { println(“Map on !-> ${thread()}”) it * 2 } .collect { println(“Collect on !-> ${thread()}”) } Flow on !-> worker-1 Map on !-> main Collect on !-> main
  35. Flow Communication val example = flow { for(i in 1!..10)

    { println(“Flow on !-> ${thread()}”) emit(i) } } example.filter { it !>= 5 } .map { println(“Map on !-> ${thread()}”) it * 2 } .flowOn(Dispatchers.Default) .collect { println(“Collect on !-> ${thread()}”) } Flow on !-> worker-1 Map on !-> worker-1 Collect on !-> main
  36. Flow Communication val example = flow { for(i in 1!..10)

    { println(“Flow on !-> ${thread()}”) emit(i) } } example.filter { it !>= 5 } .flowOn(Dispatchers.IO) .map { it * 2 } .flowOn(Dispatchers.Default) .collect { println(“Collect on !-> ${thread()}”) }
  37. Flow Communication val example = flow { withContext(Dispatchers.Default) { for(i

    in 1!..10) { emit(i) } } } example.filter { it !>= 5 } .map { it * 2 } .collect { println(“Collect on !-> ${thread()}”) } Exception in thread "main" java.lang.IllegalStateException: Flow invariant is violated: