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


battle programming a forum for elite programmers with extreme will power to sharpen theire skills
 
HomeSearchRegisterLog in
 

 kotlin coroutines timer

Go down 
AuthorMessage
kurosen
codding
codding
kurosen

Posts : 289
Join date : 2012-04-17

kotlin coroutines timer Empty
PostSubject: kotlin coroutines timer   kotlin coroutines timer EmptyFri Dec 11, 2020 11:24 am

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
Back to top Go down
kurosen
codding
codding
kurosen

Posts : 289
Join date : 2012-04-17

kotlin coroutines timer Empty
PostSubject: kotlin android studio handler tick event alternative simpler stronger code   kotlin coroutines timer EmptyFri Dec 11, 2020 1:31 pm

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
Back to top Go down
 
kotlin coroutines timer
Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
battle programmers alliance :: battle programming alliance :: battle programming-
Jump to: