简体   繁体   中英

Kotlin coroutine async with delay

I'm wrapping my head around the coroutine concept in Kotlin/Android. So, as I do not want to use Timertask, Handler with a post delayed I want to use coroutines to execute an async coroutine after a certain delay. I have the following semi-code:

 launch(UI) {
    val result = async(CommonPool) { 
        delay(30000)
        executeMethodAfterDelay() 
    }

    result.await()
 }

The problem with this is that actually in the async is both method's (delay and executeMethodAfterDelay) are executed at the same time. While I was expecting that first 30 seconds of delay would be introduced before executeMethodAfterDelay() would be fired. So my question is, how can I achieve this?

Your code is too convoluted. You need just this:

launch(UI) {
    delay(30000)
    executeMethodAfterDelay()
}

If you specifically want your method to run outside the GUI thread, then write

launch(CommonPool) {
    delay(30000)
    executeMethodAfterDelay()
}

More typically you'll want to execute a long-running method off the GUI thread and then apply its result to the GUI. This is how it's done:

launch(UI) {
    delay(30000)
    val result = withContext(CommonPool) {
        executeMethodAfterDelay()
    }
    updateGuiWith(result)
}

Note you don't need async-await in any scenario.


As for your specific report about delay running concurrently with executeMethodAfterDelay , this is not actually happening. Here's some self-contained code you can try out:

import kotlinx.coroutines.experimental.*

fun main(args: Array<String>) {
    runBlocking {
        val deferred = async(CommonPool) {
            println("Start the delay")
            delay(3000)
            println("Execute the method")
            executeMethodAfterDelay()
        }
        val result = deferred.await()
        println("Method's result: $result")
    }
}

fun executeMethodAfterDelay() = "method complete"

This will be the program's behavior:

Start the delay

... three seconds pass ...

Execute the method
Method's result: method complete

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM