$30 off During Our Annual Pro Sale. View Details »

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. 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 :
  2. 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 ?
  3. 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
  4. CPU CORE 1 Thread 1 Thread 2 Thread 3 CONCURENCY

    IN THREADS CPU CORE 2 Thread 1 Thread 2 Thread 3 @AdiPolak
  5. Kotlin runtime Coroutine 1 Coroutine 2 Coroutine 3 CONCURENCY IN

    COROUTINES Coroutine 1 Coroutine 3 @AdiPolak
  6. suspend fun someBackgroundTask(param: List<String>): Int { // long running operation

    return 7 } fun someBackgrounTask(param: List<String>, callback: Continuation<Int>): Int { // long running operation return 7 } SUSPENDING FUNCTIONS @AdiPolak
  7. Job Dispatcher COROUTINES CONTEXT AND SCOPE @AdiPolak Dispatchers.Default Dispatchers.Main Dispatchers.IO

    Dispatchers.Unconfined fun CoroutineScope.launch( context: CoroutineContext = EmptyCoroutineContext, ..): Job coroutineContext[Job]
  8. CHANNELS @AdiPolak val channel1 = Channel<Any>(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].
  9. Summary • What and why coroutines are important • Coroutine

    and threads • Key concepts • Demo • Frameworks @AdiPolak