KEMBAR78
Coroutines in Kotlin | PPTX
{ Kotlin }
Statically typed language for the JVM,
Android and the browser.
Developed by JetBrains
{ Little about the language }
● Null safety
● Expressive
● 100% java interoperability
-------------------------------------------------
● Lambdas
● ( map, filter, reduce ..)
● Higher-order functions
● Extensions
● Automatic type inference
● Inline functions
● …..
{ Coroutines }
● NOT A THREAD !!!
Is like
● Lightweight threads
● More like daemon threads
● Programs that allows to run multiple program cooperatively
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 }
{ 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)
● Launch
● Async
● Future
● Deferred
● suspend
● await
{ Coroutines }
{ Coroutines }
1 start coroutine
2 var rawData = startSomeNetworkRequest.await()
3 var data = parseData(rawData).await()
4 diplayData(data)
Create 100 000 coroutine which runs simultaneously
val jobs = List(100_100) {
async(CommonPool) {
delay(1000)
1
}
}
jobs.sumBy{ it.await() }
{ Coroutines }.example

Coroutines in Kotlin

  • 1.
    { Kotlin } Staticallytyped language for the JVM, Android and the browser. Developed by JetBrains
  • 2.
    { Little aboutthe 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 Threadpools …. + 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 VSThreads } 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.
    ● Launch ● Async ●Future ● Deferred ● suspend ● await { Coroutines }
  • 7.
    { Coroutines } 1start coroutine 2 var rawData = startSomeNetworkRequest.await() 3 var data = parseData(rawData).await() 4 diplayData(data)
  • 8.
    Create 100 000coroutine which runs simultaneously val jobs = List(100_100) { async(CommonPool) { delay(1000) 1 } } jobs.sumBy{ it.await() } { Coroutines }.example