Unresolved reference: launch

前端 未结 4 530
孤独总比滥情好
孤独总比滥情好 2021-02-05 00:58

Trying to run some examples for Kotlin coroutines, but can\'t build my project. I\'m using the latest gradle release - 4.1

Any suggestions what to check/fix?

Her

4条回答
  •  时光说笑
    2021-02-05 01:44

    Launch isn't used anymore directly. The Kotlin documentation suggests using:

    fun main() { 
        GlobalScope.launch { // launch a new coroutine in background and continue
            delay(1000L)
            println("World!")
        }
        println("Hello,") // main thread continues here immediately
        runBlocking {     // but this expression blocks the main thread
            delay(2000L)  // ... while we delay for 2 seconds to keep JVM alive
        } 
    }
    

提交回复
热议问题