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

Kotlin Coroutine to the rescue!

Kotlin Coroutine to the rescue!

Asynchronous or non-blocking programming is the new reality. Whether we're creating server-side, desktop or mobile applications, using microservices architecture or not, it provides an experience that is not only fluid from the user's perspective, but scalable when needed. Coroutines is a new feature that was released in Kotlin 1.3 and has similar counterparts in other languages like C# and JavaScript. Let’s understand together what it is all about.

adi polak

May 15, 2019
Tweet

More Decks by adi polak

Other Decks in Programming

Transcript

  1. Coroutine to the rescue! Adi Polak

    View Slide

  2. About me – Adi Polak
    @adipolak
    https://medium.com/@adipolak
    https://dev.to/adipolak

    View Slide

  3. View Slide

  4. Kotlin:
    1. Developed by JetBrains (IntelliJ authors)
    2. Powers all JetBrains tools
    3. Open source
    4. 100% interoperable with Java
    5. First language on Android
    6. Provides:
    a. Null safety
    b. Higher order function
    c. Data classes
    d. Extension functions
    e. Since Kotlin 1.1 – coroutines
    f. … MORE!
    @AdiPolak
    L E T ’ S S TA R T F R O M T H E B E G I N N I N G :

    View Slide

  5. Kotlin
    coroutines

    View Slide

  6. C o r o u t i n e s c o m m o n u s e c a s e s
    • Asynchronous
    • Non blocking
    @AdiPolak
    • Server side - microservices
    • Android app development
    • …
    W h y ?

    View Slide

  7. C a n w e c o m p a r e c o r o u t i n e
    a n d t h r e a d s ?
    @AdiPolak

    View Slide

  8. CPU CORE 1
    Thread 1 Thread 2 Thread 3
    CONCURENCY IN THREADS
    CPU CORE 2
    Thread 1 Thread 2 Thread 3
    @AdiPolak

    View Slide

  9. Kotlin runtime
    Coroutine 1 Coroutine 2 Coroutine 3
    CONCURENCY IN COROUTINES
    Coroutine 1 Coroutine 3
    @AdiPolak

    View Slide

  10. HOW?
    SUSPENDIND
    FUNCTIONS
    CHANNELS
    BUILDERS
    CONTEXT AND SCOPE
    @AdiPolak

    View Slide

  11. suspend fun someBackgroundTask(param: List): Int {
    // long running operation
    return 7
    }
    fun someBackgrounTask(param: List, callback: Continuation): Int {
    // long running operation
    return 7
    }
    SUSPENDING FUNCTIONS
    @AdiPolak

    View Slide

  12. Job
    Dispatcher
    COROUTINES CONTEXT AND SCOPE
    @AdiPolak
    Dispatchers.Default
    Dispatchers.Main
    Dispatchers.IO
    Dispatchers.Unconfined
    fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, ..): Job
    coroutineContext[Job]

    View Slide

  13. BUILDERS
    @AdiPolak
    Extending scope functions:
    Launch
    Async
    Can be used from Main thread:
    runBlocking

    View Slide

  14. COROUTINES CONTEXT, SCOPE AND BUILDERS
    @AdiPolak
    coroutineScope {
    launch(Dispatchers.Default +
    CoroutineName(”some name")) {
    doSuspendedTask()
    }
    }

    View Slide

  15. CHANNELS
    @AdiPolak
    val channel1 = Channel(capacity = Channel.RENDEZVOUS)
    val channel2 = Channel< Any >(capacity = Channel.CONFLATED)
    val channel3 = Channel< Any >(capacity = 10)
    val channel4 = Channel< Any >(capacity = Channel.UNLIMITED)
    https://proandroiddev.com/kotlin-coroutines-channels-csp-android-db441400965f
    Channel is a non-blocking primitive for communication between sender using
    [SendChannel] and receiver using [ReceiveChannel].

    View Slide

  16. Demo
    https://github.com/adipola/kotlin-coroutine-transaction-process-mock

    View Slide

  17. • Ktor
    • Vert.x
    • Arrow
    • …
    @AdiPolak
    Frameworks

    View Slide

  18. Summary
    • What and why coroutines are important
    • Coroutine and threads
    • Key concepts
    • Demo
    • Frameworks
    @AdiPolak

    View Slide

  19. Questions

    View Slide