MCE 2017: Svetlana Isakova, Coroutines in Kotlin

Поділитися
Вставка
  • Опубліковано 15 жов 2024
  • The async/await feature allows you to write the asynchronous code in a straightforward way, without a long list of callbacks. Used in C# for quite a while already, it has proved to be extremely useful. In fact, async/await is an implementation of a more general concept of coroutines, which first appeared more than 50 years ago. Coroutines are essentially light-weight threads that can be suspended and resumed later. Kotlin brings to the table the native support for coroutines, allowing to implement async/await, yield and similar programming patterns as different libraries, not built-in language constructs. The separate library for RxJava, for instance, can declare 'asyncRx' returning Observable and the 'await' function as an extension on Observable. In this talk we'll start with the essence of async/await and then proceed to the general concept of coroutines with some details of how it's implemented in Kotlin.

КОМЕНТАРІ • 10

  • @caffeinejavacode1475
    @caffeinejavacode1475 2 роки тому

    Can coroutines take executors like Completeable Future?
    If I want a separate thread for computation?

  • @bisujin1685
    @bisujin1685 7 років тому

    I cannot find UI in the launch(), but i use runOnUiThread instead

  • @alekseimulin6151
    @alekseimulin6151 7 років тому

    Ok, interesting concept but bow to cancel coroutine in the example presented?
    async {
    val image = loadImage(url).await()
    myUI.setImage(image)
    }
    And how to handle "failure" case when error happens while loading? What if we should distinguish between several type of errors e.g. server errors, no network connection, etc? Is there stack allocated for coroutines?

    • @pixelPlex
      @pixelPlex 7 років тому

      Coroutines does have its own stack for debugging purposes. The "async" function returns a "Deferred" object, which can be cancelled by calling the "cancel" function and passing through a Throwable as a parameter. Note that "await" doesn't need to be called in the "async" block. Below is a loose coupled Async/Await example:
      fun main(args: Array) {
      val deferred = (1..1_000_000).map { n ->
      async(CommonPool) { asyncWorkload(n) }
      }
      runBlocking {
      deferred.take(20).forEach { println("Num: ${it.await()}") }
      }
      }
      private suspend fun asyncWorkload(num: Int): Int {
      delay(1000)
      return num
      }

    • @ayudakov
      @ayudakov 7 років тому

      See 24:05 about exception handling. You can use regular try/catch.

    • @sprissetto
      @sprissetto 7 років тому +1

      isn't it answered at 33:50?

  • @dkozinsk
    @dkozinsk 6 років тому

    Thank you for great lecture! Would you be so kind and publish presentation slides as well?

  • @marta_na_moto
    @marta_na_moto 7 років тому +1

    Thanks for publishing this :) One small advice: its really hard to listen to it due to a hard accent of a presenter :(

    • @TheNihaonyan
      @TheNihaonyan 6 років тому

      Trust me it's not because of accent, she's just hard to listen at any language

  • @АнтонИцкович-х7у
    @АнтонИцкович-х7у 7 років тому +2

    Милаха