Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Asynchronous Programming with Kotlin

75 views

Published on

Asynchronous Programming with Kotlin by Hadi Hariri

Kotlin’s approach to asynchronous programming is about giving developers the freedom to choose what fits best. By leveraging the conventions of the language and its ability to be somewhat extended by using simple functions, Kotlin provides an asynchronous model by implementing coroutines, and provides a series of out of the box functionality such as yield, async/await as part of the standard library. In this session we’re going to see how all of this works and how Kotlin even provides us the ability to implement our own asynchronous functionality.



Published in: Technology
  • Be the first to comment

  • Be the first to like this

Asynchronous Programming with Kotlin

  1. 1. Asynchronous programming Hadi Hariri
  2. 2. Approaches
  3. 3. Threading
  4. 4. Threading • Complex - state mutations, UI threads, debugging • Unviable - single threaded environments • Limited - underlying operating system • Expensive - context switches
  5. 5. Callbacks
  6. 6. Callbacks • Complex - Error handling • Unreadable - Callback Hell { { { { { { } } } } } }
  7. 7. Promises and Futures
  8. 8. Promises and Futures • Complex - error handling can be complicated • Paradigm - new ways of combining, learning new combinators, etc.
  9. 9. Reactive Extensions interface Observable<out T> {
 fun Subscribe(observer: Observer<T>)
 } interface Observer<in T> {
 fun onNext(t: T) fun onError(e: Exception) fun onCompleted()
 }

  10. 10. Reactive Extensions • Paradigm - new ways of combining, learning new combinators, etc. Everything is a stream. And it’s observable.
  11. 11. Kotlin Coroutines
  12. 12. Do async, the same way you’d do sync
  13. 13. In essence… • Same way of thinking for sync and async • Same way of doing things (exception handling, loops, etc.) • Same constructs
  14. 14. Coroutines • Suspendable Computation • A function can be suspended, and later resumed • Co-operative multitasking a.k.a. non-preemptive • State machine with CPS • Lightweight threads
  15. 15. How they look explicit coroutine context 1 2 3 suspending function natural signature
  16. 16. How they work callback Kotlin Java/JVM
  17. 17. How they’re implemented Kotlin Java/JVM
  18. 18. Let’s see some code!
  19. 19. Thank you!

×