Ginto Philip
10/21/2021, 2:32 PMfun main(args: Array<String>) {
download()
print("from main")
}
fun download(){
runBlocking {
coroutineScope {
launch {
delay(10000)
println("from coroutine")
}
}
}
}
3. I want the "from main" to be executed first. How can I do that? . Should I call download() from a new thread?Joffrey
10/21/2021, 2:37 PMrunBlocking
is one way to give it a scope (it blocks the thread that calls it until all child coroutines are done). This is why it's the usual way to start coroutines from a main
function.
One way to do what you want is the following:
fun main(args: Array<String>) {
// runBlocking ensures we don't finish the process execution before the end of the coroutines
runBlocking {
// launch is to start a separate coroutine to run the download() suspend function
// it will start the given block asynchronously, so we immediately go to "from main"
launch {
download()
}
print("from main")
}
}
suspend fun download() {
delay(10000)
println("from coroutine")
}
Joffrey
10/21/2021, 2:39 PMdownload()
contains some blocking calls, it would be advisable to wrap these blocking calls into withContext(<http://Dispatchers.IO|Dispatchers.IO>)
so they run on a separate thread pool to avoid blocking the main threadGinto Philip
10/21/2021, 3:06 PMJoffrey
10/21/2021, 3:10 PMrunblocking{} blocks the current thread and also it provides a scopeCorrect.
Code inside the launch{} executes in the backgroundYes, it's started asynchronously so the code that follows the
launch
will run concurrently with the code that's inside the launch
. Both pieces of code could be executed on the same thread or different threads depending on the coroutine context (and especially the dispatcher in the context). In this specific example, everything will run on the main thread.
If any code needs to be executed after starting a coroutine it should be inside the runblocking{}You can also put code after
runBlocking
(if it's non-suspending) but at that point the launched coroutines will be guaranteed to be complete, so it won't run concurrently with those coroutinesGinto Philip
10/21/2021, 3:39 PMjulian
10/21/2021, 4:45 PMCode inside the launch{} executes in the background. Like starting a new thread.No, not like starting a new thread. Starting a new thread allows parallelism. Whereas, the way
launch
is used in your example (i.e without switching dispatcher), all that launch
exhibits is concurrency.