Bhaskar Singh
01/07/2025, 11:28 AMsuspend
function, the coroutine gets suspended, pausing its execution. However, the file still needs to be downloaded by some thread, meaning that the thread is actively working on the task. Why, then, can’t we say that this is blocking the thread?Joffrey
01/07/2025, 11:37 AMBhaskar Singh
01/07/2025, 11:48 AMsuspend fun downloadFiles(url : String) {
launch(Dispatcher.IO) {
download() // <----- Coroutine will suspend here and will resume after downloading a file
}
}
My confusion is My coroutine is suspended and no thread is blocked then we is downloading the fileDaniel Pitts
01/07/2025, 3:17 PMdownload
function is suspending, it will likely suspend until there is data available (often this is handle via a selector loop). If it is NOT suspend
, then it is blocking, and does indeed block the thread.Joffrey
01/07/2025, 3:50 PMwithContext
instead of launch
, but that changes quite a bit what's happening.
That being said, as Daniel said, it really depends on whether download
is blocking or suspending, and how the suspension is handled inside the function.