kurt_steiner
01/21/2024, 2:51 PMsuspend fun mydelay(timeMillis: Long) {
if (timeMillis < 0) {
return
}
if (timeMillis < Long.MAX_VALUE) {
suspendCoroutine<Unit> {
Thread.sleep(timeMillis)
it.resume(Unit)
}
}
}
but failed in
repeat(100) {
launch {
mydelay(1000)
println("awake ${Thread.currentThread()}")
}
}
it can be like delay
suspend function, how can I do it ?Ahmed
01/21/2024, 2:57 PMsuspend fun doIO() = withContext(<http://Dispatchers.IO|Dispatchers.IO>) { /* code here */ }
// If it’s something you want the result for at later in time the you can use async {} and then call .await() on it
kurt_steiner
01/21/2024, 3:03 PMdownload
, and I want to make it suspend so that I can launch many coroutines with running it
repeat(1000) {
download
}
so I similator it with Thread.sleep
Zach Klippenstein (he/him) [MOD]
01/21/2024, 7:51 PMkurt_steiner
01/22/2024, 4:11 AMZach Klippenstein (he/him) [MOD]
01/22/2024, 6:58 PMkurt_steiner
01/23/2024, 5:33 AMZach Klippenstein (he/him) [MOD]
01/23/2024, 6:33 PMsuspend fun downloadEverything() {
coroutineScope {
repeat(1000) {
launch { download() }
}
}
}
suspend fun download() {
withContext(Dispatchers.IO) {
blockingDownloadCall()
}
}