Recherche…


Introduction

Exemples d'implémentation expérimentale (encore) de coroutines de Kotlin

Coroutine simple qui retarde de 1 seconde mais pas de bloc

(extrait du document officiel)

fun main(args: Array<String>) {
    launch(CommonPool) { // create new coroutine in common thread pool
        delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
        println("World!") // print after delay
    }
    println("Hello,") // main function continues while coroutine is delayed
    Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive
}

résultat

Hello,
World!


Modified text is an extract of the original Stack Overflow Documentation
Sous licence CC BY-SA 3.0
Non affilié à Stack Overflow