kurosen codding


Posts : 289 Join date : 2012-04-17
 | Subject: kotlin coroutines timer Fri 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 } | |
|
kurosen codding


Posts : 289 Join date : 2012-04-17
 | Subject: kotlin android studio handler tick event alternative simpler stronger code Fri 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 | |
|