1 •• Rhein-Neckar Dev Meetup #1
2 •• Asynchronous progamming in Kotlin with Coroutines 🍕 🍕 🍕 Tensorflowing like a pro - without a clue
3 •• Coroutines Asynchronous programming in Kotlin
4 •• How programming works Intro Do something (create a resource) ↓ Wait (for completion) ↓ Do something (send it to backend) ↓ Wait (for the response) ↓ Do something (process response or try again) ↓ …
5 •• Dealing with long running tasks Asynchronicity - Direct style - max 16 ms - Blocking the main Thread - Callback - Error Handling - Callback Hell - Futures (RxJava, Promises, …) - Idea: we wrap the result - Pro: nice code, errors, good looking - cons: complex - Coroutines - Direct style
6 •• What is a Coroutine? - It’s an instance of a suspendable computation - General concept was already created in 1958 - Similar to a very lightweight Thread – cheap in creation - Looks like regular blocking code - Coroutines give us a way to write code that feels imperative, even if it is asynchronous.
7 •• •• Suspend functions - A function ‘super powers’ - It’s a function which can suspend and return a value later on
8 •• •• Suspend functions II - Suspension points - Many things work out-of-the box - Loops - Exceptions - Higher-Order-Functions - …
9 •• suspension functions are designed to imitate sequential behavior by default
10 •• Setup
11 •• Demo I - Sample + Performance
12 •• https://www.slideshare.net/elizarov/deep-dive-into-coroutines-on-jvm-kotlinconf-2017 Coroutines in detail
13 ••
14 ••
15 ••
16 ••
17 ••
18 ••
19 ••
20 •• Reuse closure / state object State Machine vs Callbacks Easier loop
21 •• Suspending functions cannot be invoked from regular functions • launch (fire and forget) • async (and await) • Returns a Deferred • runBlocking • Not keywords but functions! Coroutine Builder
22 •• Demo II – Coroutine Builders
23 •• Coroutine Context - *without* - UI - CommonPool - Unconfined
24 •• Demo III – Channels and Actors - Comparison RX
25 •• Questions? Pizza?
26 •• Links & References - Coroutines guide Informal - https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md - Coroutine guide reactive - https://github.com/Kotlin/kotlinx.coroutines/blob/master/reactive/coroutines-guide-reactive.md - Deep dive into Coroutines on JVM, KotlinConf 2017 – Roman Elizarov https://www.slideshare.net/elizarov/deep-dive-into-coroutines-on-jvm-kotlinconf-2017 - Images from https://www.pexels.com

Asynchronous Programming in Kotlin with Coroutines