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

Coroutines in Kotlin

Coroutines in Kotlin

Droid UP 6.0 Tbilisi, Gerorgia
Little about Kotlin and its new awesome feature Coroutines

Jemo Mgebrishvili

March 27, 2017
Tweet

More Decks by Jemo Mgebrishvili

Other Decks in Programming

Transcript

  1. { Kotlin } Statically typed language for the JVM, Android

    and the browser. Developed by JetBrains
  2. { Little about the language } • Null safety •

    Expressive • 100% java interoperability ------------------------------------------------- • Lambdas • ( map, filter, reduce ..) • Higher-order functions • Extensions • Automatic type inference • Inline functions • …..
  3. { Coroutines } • NOT A THREAD !!! Is like

    • Lightweight threads • More like daemon threads • Programs that allows to run multiple program cooperatively
  4. Behind the scene ExecutorService ThreadPoolExecutor Executors Thread pools …. +

    Kotlin implementation of pools, CoroutineDispatcher and etc • Write asynchronous code as it was synchronous • Avoid from callback hell • Run multiple async computations simultaneously(demo in example) • Less memory usage (compared to Threads) { Coroutines }
  5. { Coroutines VS Threads } Coroutine - • It is

    stackless(doesn't have own stack) low memory usage • Doesn't map on native thread • Managed by user • Cooperatively multitasking (no context switch)
  6. { Coroutines } 1 start coroutine 2 var rawData =

    startSomeNetworkRequest.await() 3 var data = parseData(rawData).await() 4 diplayData(data)
  7. Create 100 000 coroutine which runs simultaneously val jobs =

    List(100_100) { async(CommonPool) { delay(1000) 1 } } jobs.sumBy{ it.await() } { Coroutines }.example