Channels N Stuff

Channels N Stuff

Let's dig into kotlin coroutines channels.
Thats where it started, it ended up with a crash-course in coroutines, synchronisation primitives, deferred, channels and Flow.
Also trying to answer the question - will it replace Rx?

0297b9b4bfd45c0f9c6c52bf696b7735?s=128

Bob Dahlberg

November 16, 2019
Tweet

Transcript

  1. 
 Channels N Stuff Bob
 Dahlberg

  2. Bob Dahlberg Android at Bonnier News
 Challenge Accepted Kotlin Stockholm

    Tech Group GDG Stockholm Android Who’s Bob?
 - Building software
 - CrossFit enthusiast
 - Father of one
 - Arranging meetups
 - Loves a good challenge!
  3. Background ActionScript (Flash) Been there! Done that! Moved … 


  4. Background ActionScript (Flash) Been there! Done that! Moved … For

    all of you born after the millennium, flash was king!
  5. Background ActionScript (Flash)

  6. Background ActionScript Android (Java) Started something new since flash was

    about to be abandoned. Went native with Java rather than cross platform: 
 (ActionScript, Xamarin, PhoneGap)

  7. Background ActionScript Android (Java) Did a lot of threading, networking,

    image loading with different solutions. Built my own async task handler.
  8. Background ActionScript Android Rx Did a lot of threading, networking,

    image loading with different solutions. Built my own async task handler. Ended up doing a lot of Rx.
  9. Background ActionScript Android Rx Scala Learned Scala and tried to

    build Android with it..
  10. Background ActionScript Android Rx Scala 
 Learned Scala and tried

    to build Android with it.. It’s plausible.
 But you rather die a little.
  11. Background ActionScript Android Rx Scala Kotlin Found Kotlin in 2015

    and it blended almost seamlessly with Java.

  12. Background ActionScript Android Rx Scala Kotlin So why all this

    about my past?
  13. Background ActionScript - Asynchronous Android - Asynchronous Rx - Asynchronous

    Scala. - Functional Kotlin - Functional I’m raised in an asynchronous, callback world. With strong beliefs in reactive and functional idioms. Even before I knew it.
  14. Background ActionScript - Asynchronous Android - Asynchronous Rx - Asynchronous

    Scala. - Functional Kotlin - Functional Cause of that coroutines are hard for my brain to grasp.
  15. Coroutines

  16. Coroutines Lightweight threads Conceptually the same as threads. - Offload

    async work on them
 - Specific ways of communication
  17. Coroutines Lightweight threads Structured 
 Structured concurrency “An outer coroutine

    does not complete until all the coroutines launched in its scope complete” 
 
 
 in its scope
  18. Coroutines Lightweight threads Structured

  19. Coroutines Lightweight threads Structured

  20. Coroutines Lightweight threads Structured

  21. Coroutines Lightweight threads Structured

  22. Coroutines Lightweight threads Structured Suspend 
 Coroutines are built by

    suspending functions. They suspend the current scope until done.
  23. Coroutines Lightweight threads Structured Suspend

  24. Coroutines Lightweight threads Structured Suspend

  25. Coroutines Lightweight threads Structured Suspend

  26. Coroutines Lightweight threads Structured Suspend Sequential 
 Coroutines are sequential

    by default. Non blocking, asynchronous.
 But not concurrent.
  27. Coroutines Lightweight threads Structured Suspend Sequential

  28. Coroutines Lightweight threads Structured Suspend Sequential Dispatchers “The coroutine context

    includes a coroutine dispatcher that determines what thread or threads the corresponding coroutine uses for its execution.”
  29. Coroutines Lightweight threads Structured Suspend Sequential Dispatchers

  30. Coroutines Lightweight threads Structured Suspend Sequential Dispatchers

  31. Coroutines Lightweight threads Structured Suspend Sequential Dispatchers Dispatchers.Default - 8

    threads
 Dispatchers.IO - 64 threads
 Dispatchers.Main - 0 or 1 threads
  32. Coroutines Lightweight threads Structured Suspend Sequential Dispatchers Dispatchers.Default - 8

    threads
 Dispatchers.IO - 64 threads
 Dispatchers.Main - 0 or 1 threads On this machine!
  33. Coroutines Comparison

  34. Comparison Callbacks

  35. Comparison Callbacks Rx

  36. Comparison Callbacks Rx Coroutines

  37. So are coroutines replacing Rx?!

  38. So are coroutines replacing Rx?! Nope!

  39. So are coroutines replacing Rx?! Nope! It can replace some.

    
 But coroutines is not reactive streams.
  40. Coroutines Sharing is caring

  41. Communication Shared mutable state 
 Still something that should be

    handled with extreme caution! Coroutines gives you nothing new on that matter.
  42. Communication Shared mutable state launch vs async Both launch and

    async coroutine builders returning a job for the calling scope to control 
 (cancel - join).
  43. Communication Shared mutable state launch vs async

  44. Communication Shared mutable state launch vs async But async gives

    us a bit more.
  45. Communication Shared mutable state launch vs async Deferred

  46. Communication Shared mutable state launch vs async Deferred

  47. Communication Shared mutable state launch vs async Deferred

  48. Communication Shared mutable state launch vs async Deferred

  49. Communication Shared mutable state launch vs async Deferred Order

  50. Communication Shared mutable state launch vs async Deferred Order

  51. Communication Shared mutable state launch vs async Deferred Order

  52. Communication Shared mutable state launch vs async Deferred Order

  53. Communication Shared mutable state launch vs async Deferred Order

  54. Communication Shared mutable state launch vs async Deferred Order Streams

  55. Communication Shared mutable state launch vs async Deferred Order Streams

  56. Channels

  57. Channels Definition “Deferred values provide a convenient way to transfer

    a single value between coroutines. 
 
 Channels provide a way to transfer a stream of values.”
  58. Channels Definition
 Streams revisited

  59. Channels Definition
 Streams revisited

  60. Channels Definition
 Streams revisited

  61. Channels Definition
 Streams revisited Order revisited

  62. Channels Definition
 Streams revisited Order revisited

  63. Channels Definition
 Streams revisited Order revisited

  64. Channels Definition
 Streams revisited Order revisited

  65. Channels Definition
 Streams revisited Order revisited

  66. Channels Definition
 Streams revisited Order revisited Order revisited 2

  67. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3
  68. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3 Plain order of execution should not be handled by channels.
  69. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3 But Channels are synchronization primitives…
  70. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3 But Channels are synchronization primitives… So there must be some order!
  71. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3 Order - Producer / Consumer
  72. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3 Order - Producer / Consumer
  73. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3 Order - Producer / Consumer
  74. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3 Order - Producer / Consumer
  75. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3 Order - Producer / Consumer Experimental
  76. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3 Order - Producer / Consumer Experimental
  77. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

    revisited 3 Order - Producer / Consumer Experimental A friend once told me that if it’s on a single line it can’t be bad code.
  78. Channels Will it terminate?

  79. Will it terminate

  80. Will it terminate

  81. Will it terminate

  82. Will it terminate

  83. Will it terminate

  84. Gotchas

  85. Gotchas Send is suspending

  86. Gotchas Send is suspending Buffer

  87. Gotchas Send is suspending Buffer Rendezvous

  88. Gotchas Send is suspending Buffer Rendezvous Unlimited

  89. Gotchas Send is suspending Buffer Rendezvous Unlimited Conflated

  90. Gotchas Send is suspending Buffer Rendezvous Unlimited Conflated Channels are

    hot
  91. Channels “Primitive” usages

  92. Channels Fan out One producer, many consumers.

  93. Channels Fan out

  94. Channels Fan out

  95. Channels Fan out Fan in One consumer, many producers.

  96. Channels Fan out Fan in

  97. Channels Fan out Fan in

  98. Channels Fan out Fan in FIFO Channels respect the order

    of invocations from different coroutines. Elements are received in a “first in first out” order.
  99. Channels Fan out Fan in FIFO Channels respect the order

    of invocations from different coroutines. Elements are received in a “first in first out” order.
  100. Channels Builders and Types of Channels

  101. Creating Channel The simple Channel() standard and simple. Also ReceiverChannel

    and SendChannel for specific usage
  102. Creating Channel Broadcast? As seen especially in the fan out

    example where we have many consumers and one producer, kinda like RX. But there’s only one receiving each element… can we broadcast?
  103. Creating Channel BroadcastChannel As seen especially in the fan out

    example where we have many consumers and one producer, kinda like RX. But there’s only one receiving each element… can we broadcast? Yes, we can!
  104. Creating Channel BroadcastChannel
 ConflatedBroadcastChannel Now we can also have only

    the most recent sent value. Did anyone say BehaviorSubject?
  105. Creating Channel BroadcastChannel
 ConflatedBroadcastChannel Operators? To really become a force

    to compare to Rx we must have some operators!
  106. Creating Channel BroadcastChannel
 ConflatedBroadcastChannel Operators 
 .distinct
 
 .filter
 


    .flatMap
 
 .reduce
  107. Creating Channel BroadcastChannel
 ConflatedBroadcastChannel Operators .any
 .distinct
 .drop
 .filter
 .first


    .flatMap
 .minBy
 .reduce
  108. Creating Channel BroadcastChannel
 ConflatedBroadcastChannel Operators That’s awesome! .any
 .distinct
 .drop


    .filter
 .first
 .flatMap
 .minBy
 .reduce And 87 others.
  109. Is it a replacement for Rx?

  110. Is it a replacement for Rx? Let’s investigate the signs.

  111. Creating Channel BroadcastChannel
 ConflatedBroadcastChannel Operators That’s awesome! .any
 .distinct
 .drop


    .filter
 .first
 .flatMap
 .minBy
 .reduce And 87 others.
  112. Creating Channel BroadcastChannel
 ConflatedBroadcastChannel Operators That’s awesome! .any
 .distinct
 .drop


    .filter
 .first
 .flatMap
 .minBy
 .reduce And 87 others. Experimental
  113. Creating Channel BroadcastChannel
 ConflatedBroadcastChannel Operators That’s awesome! .any
 .distinct
 .drop


    .filter
 .first
 .flatMap
 .minBy
 .reduce And 87 others. DEPRECATED
  114. Is it a replacement for Rx? Nope

  115. Channels are synchronization primitives

  116. Actor Should match Like actors that encapsulates a shared mutable

    state. Their mailbox should be a good fit for channels!
  117. Actor Should match Actor builder

  118. Actor Should match Actor builder

  119. Actor Should match Actor builder

  120. ObsoleteApi Meaning it will be removed

  121. Why?

  122. Why? Why is everything deprecated or obsolete?

  123. Why? Actor builder “Note: This API will become obsolete in

    future updates with introduction of complex actors.”
  124. Why? Actor builder All the operators “Channel operators are deprecated

    in favour of Flow and will be removed in 1.4”
  125. What is Flow?

  126. Flow Cold streams Cold streams, some using channels to communicate.

  127. Flow Cold streams On coroutines Running on coroutines and benefits

    from not having subscribers and cleaning up duties.
  128. Flow Cold streams On coroutines Extensible Loads of operators built

    in 
 and extremely easy to add 
 via extension functions.
  129. Flow Cold streams On coroutines Extensible Channels Giving them the

    benefit of back pressure support easy as that.
  130. Is it a replacement for Rx?

  131. Is it a replacement for Rx?

  132. Is it a replacement for Rx? Yup! (IMHO)

  133. To sum up

  134. Sum up Coroutines 
 Not a replacement of Rx


  135. Sum up Coroutines Channels 
 Not a replacement of Rx


    Not a replacement of Rx
  136. Sum up Coroutines Channels Flow 
 
 Not a replacement

    of Rx
 Not a replacement of Rx 
 A replacement of Rx
  137. Take aways Coroutines It’s one of the cleanest ways to

    work with asynchronous code. Even though I’m still having issues to wrap my head around the new way of thinking. You know, old dogs!
  138. Take aways Coroutines Deferred 
 
 A synchronisation primitive solving

    the output of one value or one object from a coroutine.

  139. Take aways Coroutines Deferred Channel A synchronisation primitive solving the

    output of a stream of values or objects from one or several coroutine to one or several coroutines.

  140. Take aways Coroutines Deferred Channel Hot streams

  141. Take aways Coroutines Deferred Channel Buffering strategies

  142. Take aways Coroutines Deferred Channel Producer - Consumer

  143. Take aways Coroutines Deferred Channel Actors

  144. Take aways Coroutines Deferred Channel Flow The next thing in

    asynchronous programming with Kotlin that will really give other reactive stream solutions a match.
  145. Take aways Coroutines Deferred Channel Flow Kotlin Conf: - ASYNCHRONOUS

    DATA STREAMS 
 WITH KOTLIN FLOW
 - MIGRATING FROM RXJAVA TO FLOW
 - INTRO TO CHANNELS AND FLOW
  146. Thank you! Kotlin Conf: - ASYNCHRONOUS DATA STREAMS 
 WITH

    KOTLIN FLOW
 - MIGRATING FROM RXJAVA TO FLOW
 - INTRO TO CHANNELS AND FLOW