battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.

battle programmers allianceLog in

battle programming a forum for elite programmers with extreme will power to sharpen their skills

descriptionkotlin coroutines timer Emptykotlin coroutines timer

more_horiz
simple kotlin coroutines timer with async jutsu

Code:

fun main(){
    exampleAsyncAwait()
    println("main thread end")
}
suspend fun calculateHardThings(startNum: String){
    delay(1000)
    println(startNum)
}
fun exampleAsyncAwait() = runBlocking {
    val startTime = System.currentTimeMillis()
    var c1:Int=1;
    launch { while (c1<10){
        c1++;
        val deferred1 = async { calculateHardThings("tick") }
        val deferred2 = async { calculateHardThings("tok") }.await()
    } }

    println("end")//main code thread, does not wait for the timer which is in the BackGround
}


study

descriptionkotlin coroutines timer Emptykotlin android studio handler tick event alternative simpler stronger code

more_horiz
kotlin android studio handler tick event alternative simpler stronger code :

Code:

fun main()= runBlocking{

    var c1:Int=1;
    launch (Dispatchers.Default){ while (c1<10){
        c1++;
        val deferred1 = async { tic("tick") }
        val deferred2 = async { tic("tok") }.await()
    } }
    println("main thread end")
}
suspend fun tic(startNum: String){
    delay(1000)
    println(startNum)
}


output :

main thread end
tick
tok
tick
tok
tok
tick
tok
tick
tick
tok
tok
tick
tick
tok
tick
tok
tok
tick

Process finished with exit code 0
emp it up admin hisatsu
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum