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

Channels N Stuff

Bob Dahlberg
November 16, 2019

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?

Bob Dahlberg

November 16, 2019
Tweet

More Decks by Bob Dahlberg

Other Decks in Programming

Transcript

  1. 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!
  2. Background ActionScript (Flash) Been there! Done that! Moved … For

    all of you born after the millennium, flash was king!
  3. 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)

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

    image loading with different solutions. Built my own async task handler.
  5. 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.
  6. Background ActionScript Android Rx Scala 
 Learned Scala and tried

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

    and it blended almost seamlessly with Java.

  8. 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.
  9. Background ActionScript - Asynchronous Android - Asynchronous Rx - Asynchronous

    Scala. - Functional Kotlin - Functional Cause of that coroutines are hard for my brain to grasp.
  10. Coroutines Lightweight threads Conceptually the same as threads. - Offload

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

    does not complete until all the coroutines launched in its scope complete” 
 
 
 in its scope
  12. Coroutines Lightweight threads Structured Suspend 
 Coroutines are built by

    suspending functions. They suspend the current scope until done.
  13. Coroutines Lightweight threads Structured Suspend Sequential 
 Coroutines are sequential

    by default. Non blocking, asynchronous.
 But not concurrent.
  14. 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.”
  15. Coroutines Lightweight threads Structured Suspend Sequential Dispatchers Dispatchers.Default - 8

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

    threads
 Dispatchers.IO - 64 threads
 Dispatchers.Main - 0 or 1 threads On this machine!
  17. So are coroutines replacing Rx?! Nope! It can replace some.

    
 But coroutines is not reactive streams.
  18. Communication Shared mutable state 
 Still something that should be

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

    async coroutine builders returning a job for the calling scope to control 
 (cancel - join).
  20. 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.”
  21. Channels Definition
 Streams revisited Order revisited Order revisited 2 Order

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

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

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

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

    revisited 3 Order - Producer / Consumer Experimental
  26. 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.
  27. 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.
  28. 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.
  29. 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?
  30. 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!
  31. Creating Channel BroadcastChannel
 ConflatedBroadcastChannel Now we can also have only

    the most recent sent value. Did anyone say BehaviorSubject?
  32. Creating Channel BroadcastChannel
 ConflatedBroadcastChannel Operators That’s awesome! .any
 .distinct
 .drop


    .filter
 .first
 .flatMap
 .minBy
 .reduce And 87 others. Experimental
  33. Actor Should match Like actors that encapsulates a shared mutable

    state. Their mailbox should be a good fit for channels!
  34. Why? Actor builder “Note: This API will become obsolete in

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

    in favour of Flow and will be removed in 1.4”
  36. Flow Cold streams On coroutines Running on coroutines and benefits

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

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

    benefit of back pressure support easy as that.
  39. Sum up Coroutines Channels Flow 
 
 Not a replacement

    of Rx
 Not a replacement of Rx 
 A replacement of Rx
  40. 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!
  41. Take aways Coroutines Deferred 
 
 A synchronisation primitive solving

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

  42. 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.

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

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

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

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