kotlin coroutines precedence of execution - kotlin

I need help understanding the outcome of the following 2 pieces of code
1st snippet
fun main() = runBlocking {
launch {
println("Task from runBlocking")
}
coroutineScope {
launch {
println("Task from nested launch")
}
println("Task from coroutine scope")
}
println("Coroutine scope is over")
}
And, the 2nd snippet
fun main() = runBlocking {
launch {
println("Task from runBlocking")
}
println("Coroutine scope is over")
}
The outcome of the 1st snippet is:
Task from coroutine scope
Task from runBlocking
Task from nested launch
Coroutine scope is over
The outcome of the 2nd snippet is:
Coroutine scope is over
Task from runBlocking
So, the question is why were the statements printed in that order?

For the 1st snippet you are using coroutineScope which, as you can see from the documentation is defined as a suspend function, so it's blocking the current thread and "Coroutine scope is over" is not printed until coroutineScope block has completed.
For the 2nd snippet the string "Coroutine scope is over" is printed before "Task from run blocking" because its println it's executed in the main thread, and launch which runs in a worker thread has not finished yet its job.
fun mainOne() = runBlocking {
launch {
println("Task from runBlocking")
}
// coroutineScope is defined as a suspend function, so it's blocking the current thread
coroutineScope {
launch {
// worker thread, slower than main thread, printed after "Task from coroutine scope"
println("Task from nested launch")
}
// main thread, printed before "Task from nested launch" within CoroutineScope
println("Task from coroutine scope")
}
// main thread is now free to run and it prints the string below
println("Coroutine scope is over")
}
fun mainTwo() = runBlocking {
launch {
// worker thread, slower than main thread, printed after "Coroutine scope is over" due to concurrency
println("Task from runBlocking")
}
// main thread is running and prints the string below before launch job has finished.
// If you put a delay here you'll notice that launch job gets completed before "Coroutine scope is over"
// E.g delay(2000)
println("Coroutine scope is over")
}
Hope this makes sense :)

Calling launch just puts a task on the queue while calling coroutineScope suspends the current coroutine until the new coroutineScope completes.
This is what happens in Snippet 2:
puts "Task from runBlocking" on queue
prints "Coroutine scope is over"
executes sole task from queue (prints "Task from runBlocking")
This is what happens in Snippet 1
puts "Task from runBlocking" on queue
coroutineScope (a suspend function) is called! The suspend function is called immediately and the mainCoroutine will not proceed until the suspend function completes
puts "Task from nested launch" on queue
prints "Task from coroutine scope"
suspends execution since coroutineScope can't complete until it's child in the queue completes
first task in queue is executed "Task from runBlocking"
second task in queue is executed "Task from nested launch"
new CoroutineScope completes since its child task just completed! Back to main coroutine!
prints "Coroutine scope is over"

Related

Is coroutineScope just like block function in Kotlin? [duplicate]

I was reading Coroutine Basics trying to understand and learn it.
There is a part there with this code:
fun main() = runBlocking { // this: CoroutineScope
launch {
delay(200L)
println("Task from runBlocking")
}
coroutineScope { // Creates a new coroutine scope
launch {
delay(900L)
println("Task from nested launch")
}
delay(100L)
println("Task from coroutine scope") // This line will be printed before nested launch
}
println("Coroutine scope is over") // This line is not printed until nested launch completes
}
The output goes like so:
Task from coroutine scope
Task from runBlocking
Task from nested launch
Coroutine scope is over
My question is why this line:
println("Coroutine scope is over") // This line is not printed until nested launch completes
is called always last?
Shouldn't it be called since the:
coroutineScope { // Creates a new coroutine scope
....
}
is suspended?
There is also a note there:
The main difference between runBlocking and coroutineScope is that the latter does not block the current thread while waiting for all children to complete.
I dont understand how coroutineScope and runBlocking are different here? coroutineScope looks like its blocking since it only gets to the last line when it is done.
Can anyone enlighten me here?
I don't understand how coroutineScope and runBlocking are different here? coroutineScope looks like its blocking since it only gets to the last line when it is done.
There are two separate worlds: the suspendable world (within a coroutine) and the non-suspendable one. As soon as you enter the body of runBlocking, you are in the suspendable world, where suspend funs behave like blocking code and you can't get to the next line until the suspend fun returns. coroutineScope is a suspend fun that returns only when all the coroutines inside it are done. Therefore the last line must print at the end.
I copied the above explanation from a comment which seems to have clicked with readers. Here is the original answer:
From the perspective of the code in the block, your understanding is correct. The difference between runBlocking and coroutineScope happens at a lower level: what's happening to the thread while the coroutine is blocked?
runBlocking is not a suspend fun. The thread that called it remains inside it until the coroutine is complete.
coroutineScope is a suspend fun. If your coroutine suspends, the coroutineScope function gets suspended as well. This allows the top-level function, a non-suspending function that created the coroutine, to continue executing on the same thread. The thread has "escaped" the coroutineScope block and is ready to do some other work.
In your specific example: when your coroutineScope suspends, control returns to the implementation code inside runBlocking. This code is an event loop that drives all the coroutines you started within it. In your case, there will be some coroutines scheduled to run after a delay. When the time arrives, it will resume the appropriate coroutine, which will run for a short while, suspend, and then control will be again inside runBlocking.
While the above describes the conceptual similarities, it should also show you that runBlocking is a completely different tool from coroutineScope.
runBlocking is a low-level construct, to be used only in framework code or self-contained examples like yours. It turns an existing thread into an event loop and creates its coroutine with a Dispatcher that posts resuming coroutines to the event loop's queue.
coroutineScope is a user-facing construct, used to delineate the boundaries of a task that is being parallel-decomposed inside it. You use it to conveniently await on all the async work happening inside it, get the final result, and handle all failures at one central place.
The chosen answer is good but fails to address some other important aspects of the sample code that was provided. For instance, launch is non-blocking and is suppose to execute immediately. That is simply not true. The launch itself returns immediately BUT the code inside the launch does appear to be put into a queue and is only executed when any other launches that were previously put into the queue have completed.
Here's a similar piece of sample code with all the delays removed and an additional launch included. Without looking at the result below, see if you can predict the order in which the numbers are printed. Chances are that you will fail:
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
println("1")
}
coroutineScope {
launch {
println("2")
}
println("3")
}
coroutineScope {
launch {
println("4")
}
println("5")
}
launch {
println("6")
}
for (i in 7..100) {
println(i.toString())
}
println("101")
}
The result is:
3
1
2
5
4
7
8
9
10
...
99
100
101
6
The fact that number 6 is printed last, even after going through nearly 100 println have been executed, indicates that the code inside the last launch never gets executed until all non-blocking code after the launch has completed. But that is not really true either, because if that were the case, the first launch should not have executed until numbers 7 to 101 have completed. Bottom line? Mixing launch and coroutineScope is highly unpredictable and should be avoided if you expect a certain order in the way things should be executed.
To prove that code inside launches is placed into a queue and ONLY executed after ALL the non-blocking code has completed, run this (no coroutineScopes are used):
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
println("1")
}
launch {
println("2")
}
launch {
println("3")
}
for (i in 4..100) {
println(i.toString())
}
println("101")
}
This is the result you get:
4
5
6
...
101
1
2
3
Adding a CoroutineScope will break this behavior. It will cause all non-blocking code that follows the CoroutineScope to not be executed until ALL code prior to the CoroutineScope has completed.
It should also be noted that in this code sample, each of the launches in the queue are executed sequentially in the order that they are added to the queue and each launch will only execute AFTER the previous launch executes. This may make it appear that all launches share a common thread. This is not true. Each of them is given their own thread. However, if any code inside a launch calls a suspend function, the next launch in the queue is started immediately while the suspend function is being carried out. To be honest, this is very strange behavior. Why not just run all the launches in the queue asynchronously? While I don't know the internals of what goes on in this queue, my guess is that each launch in the queue does not get its own thread but all share a common thread. It is only when a suspend function is encountered does it appear that a new thread is created for the next launch in the queue. It may be done this way to save on resources.
To summarize, execution is done in this order:
Code inside a launch is placed inside a queue and are executed in the order that they are added.
Non-blocking code following a launch executes immediately before anything in the queue is executed.
A CoroutineScope blocks ALL code following it BUT will execute all the launch coroutines in the queue before resuming to the code following the CoroutineScope.
runBlocking is for you to block the main thread.
coroutineScope is for you to block the runBlocking.
Well, after having read all the answers here, I found none of them answered the question beyond repeating the wording of the fragments of the documentation.
So, I went on to search for an answer elsewhere and found it here. It practically shows the difference in behavior of coroutineScope and runBlocking (i.e. the difference between suspending and blocking)
runBlocking just blocks the current thread until inner coroutines will be completed. Here, thread that executes runBlocking will be blocked until the coroutine from coroutineScope will be finished.
First launch just won't allow the thread execute instructions that come after runBlocking, but will allow proceed to the instructions that come immediately after this launch block - that's why Task from coroutine scope is printed before than Task from runBlocking.
But nested coroutineScope in the context of runBlocking won't allow the thread to execute instructions that come after this coroutineScope block, because runBlocking will block the thread until the coroutine from coroutineScope will be finished completely. And that's why Coroutine scope is over will always come after Task from nested launch.
From this wonderful article https://jivimberg.io/blog/2018/05/04/parallel-map-in-kotlin/
suspend fun <A, B> Iterable<A>.pmap(f: suspend (A) -> B): List<B> = coroutineScope {
map { async { f(it) } }.awaitAll()
}
With runBlocking, we were not using Structured Concurrency, so an invocation of f could fail and all other executions would continue unfazed. And also we were not playing nice with the rest of the code. By using runBlocking we were forcefully blocking the thread until the whole execution of pmap finishes, instead of letting the caller decide how the execution should go.

Cancelling a non-cancelable coroutine in Kotlin

Consider this non cancellable coroutine that works as its name implies.
fun main(args: Array<String>) = runBlocking {
val nonCancellableJob = launch(Dispatchers.Default) {
for (i in 1..1000) {
if (i % 100 == 0) {
println("Non cancellable iteration $i")
}
}
}
println("Cancelling non cancellable job...")
nonCancellableJob.cancelAndJoin()
}
Now, if I get rid of the explicit dispatcher Dispatchers.Default and use the inherited one i.e. launch {...} the coroutine gets cancelled immediately without printing anything. It seems that a non cancelling coroutine is being cancelled! Is it a bug or what?
When you don't use Dispatchers.Default then launch is inheriting dispatcher of runBlocking which seems to defer execution until needed.
Even though your coroutine cannot be canceled while running (since it doesn't have any activity check nor does it have suspension points) it's still possible to cancel it before it starts executing.
You can modify CoroutineStart of your launch builder to alter this behavior:
To execute it immediately when declared:
val nonCancellableJob = launch(start = CoroutineStart.UNDISPATCHED) { ... }
To prevent cancellation before it starts:
val nonCancellableJob = launch(start = CoroutineStart.ATOMIC) { ... }
The inherited dispatcher runs on the same thread where you launched it. This works such that runBlocking establishes a top-level loop that goes over the coroutines running inside it, resuming them one by one. It can't resume the next coroutine before the current one has suspended itself.
In your code, the top-level runBlocking coroutine created one child coroutine and then, without suspending, went on to do some more things: print a message and cancel the coroutine it just created. At this point the coroutine is still hanging in the limbo of having been created but not run. Your cancelAndJoin call cancels it in this state, before it has got its chance to hog the runBlocking dispatcher forever. It immediately changes the state to "completed through cancellation" and the whole program ends.

Coroutine Execution Order using withContext

I started looking into Coroutines a few days ago. I understand a little bit, but then I saw some lines of code that raised some questions:
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
println("in sub coroutine ${Thread.currentThread().name}")
}
println("before coroutine in main ${Thread.currentThread().name}")
withContext(Dispatchers.IO) {
println("hello from coroutine ${Thread.currentThread().name}")
delay(1500)
println("hello from coutoutine after delay ${Thread.currentThread().name}")
}
println("after coroutine in main ${Thread.currentThread().name}")
}
The output is:
before coroutine in main main #coroutine#1
in sub coroutine main #coroutine#2
hello from coroutine DefaultDispatcher-worker-1 #coroutine#1
hello from coutoutine after delay DefaultDispatcher-worker-1 #coroutine#1
after coroutine in main main #coroutine#1
From my understanding, launch creates a new coroutine on the worker thread, thus any normal function on main thread is executed before the launch is completed. If so, I am a bit confused why the withContext code runs before the launch code. Can someone explain?
launch creates a new coroutine on the worker thread
Be careful when you frame your thoughts according to a sentence like this. A coroutine doesn't run on a given thread the way normal code does. It is much more like pinning a thread to a CPU core. The pinned thread doesn't own the core, the OS just makes sure that, whenever it suspends and then resumes it, it will schedule it to the same CPU core.
If you go through your code with the "scheduling threads to CPU cores" paradigm, you can easily see how your output makes sense:
runBlocking { // Create a context within which "threads" are pinned
// to a single "core", let's call it "Main Core"
launch { // Start another "thread" pinned to "Main Core". The "thread" is
// in a suspended state, waiting for "Main Core" to get free
println("in sub coroutine ${Thread.currentThread().name}")
}
// `launch` is just a function, it completed after creating the new "thread",
// move on to the code below it
println("before coroutine in main ${Thread.currentThread().name}")
// Start a context where "threads" are pinned to another "core", the
// "IO Core". It executes its "threads" concurrently to "Main Core".
// However, the particular "thread" that creates the context gets suspended
// until it is done. Other "threads" pinned to "Main Core" can run.
withContext(Dispatchers.IO) {
println("hello from coroutine ${Thread.currentThread().name}")
delay(1500)
println("hello from coutoutine after delay ${Thread.currentThread().name}")
}
// Now the "thread" that created the "IO Core" context can go on.
println("after coroutine in main ${Thread.currentThread().name}")
}
To this picture you just have to add the fact that the "OS" is unable to preemptively suspend a "thread", only when the "thread" suspends itself the "OS" can take over to make another scheduling decision.

how coroutine scope builder flow works

Kotlin says
runBlocking method blocks the current thread for waiting
coroutineScope just suspends, releasing the underlying thread for other usages.
hence runBlocking is a regular function and coroutineScope is a suspending function
fun main() = runBlocking { // this: CoroutineScope
launch {
delay(200L)
println("Task from runBlocking")
}
coroutineScope { // Creates a coroutine scope
launch {
delay(500L)
println("Task from nested launch")
}
delay(100L)
println("Task from coroutine scope") // This line will be printed before the nested launch
}
println("Coroutine scope is over") // This line is not printed until the nested launch completes
}
in above example what i expect is :-
runBlocking blocks main thread and launch will executed and it comes to delay(200L)
So, underlying coroutine is released and runs coroutineScope and comes to delay(500L) & delay(100L)
So, again underlying coroutine is released and it should print println("Coroutine scope is over").
This is what my understanding on runBlocking and coroutineScope. Which is not working as expected.
the Output is
Task from coroutine scope
Task from runBlocking
Task from nested launch
Coroutine scope is over
Can anyone kindly explain in a easy way to understand this.
launch causes the block to be executed asynchronously, so the call to launch returns immediately, and the coroutine continues its running and doesn't wait for the execution of the launched block.
Therefore, immediately after runBlocking is called, the first and the second launch are called one after another, and immediately after that the coroutine is suspended on delay(100L).
After 100ms the coroutine is resumed and prints "Task from coroutine scope", and then the execution of the nested coroutine-scope's block ends. A coroutine-scope always waits for the end of execution of all the jobs it has launched, so it waits here for 500ms.
Meanwhile, the two launched blocks are executed, so "Task from runBlocking" is printed first (after 200ms from the start), and then "Task from nested launch" is printed (after 500ms from the start).
Eventually, after the internal launched job has been completed, the internal coroutine-scope finishes waiting, and the external coroutine continues and prints "Coroutine scope is over".
This is the story. I hope it helps a little to understand how the code is executed and why the order of printing is like that.
I modified the code a little
fun main() = runBlocking(Dispatchers.Default) {
var i = 1
launch {
println("Task from runBlocking")
while (i < 10) {
delay(30L)
println(i++)
}
}
coroutineScope { // Creates a coroutine scope
launch {
delay(200L)
println("Task from nested launch")
}
delay(100L)
println("Task from coroutine scope") // This line will be printed before the nested launch
}
println("Coroutine scope is over")
}
Output
Task from runBlocking
1
2
3
Task from coroutine scope
4
5
6
Task from nested launch
Coroutine scope is over
7
8
9
the observation i made is,
delay(100L) is equal to approximate 3 times delay(30L)
delay(200L) is equal to approximate 6 times delay(30L)
So, after 3 Task from coroutine scope and after 6 Task from nested launch is printed.
then exactly after this Coroutine scope is over but you can still see the loop printed 7,8,9.
This is because like runBlocking coroutineScope waits for all its members to execute by suspending underlying threads. But understand, those threads first work on members of coroutineScope not on runBlocking.
Hence, it is printing Task from coroutine scope and Task from nested launch before Coroutine scope is over

Coroutines: runBlocking vs coroutineScope

I was reading Coroutine Basics trying to understand and learn it.
There is a part there with this code:
fun main() = runBlocking { // this: CoroutineScope
launch {
delay(200L)
println("Task from runBlocking")
}
coroutineScope { // Creates a new coroutine scope
launch {
delay(900L)
println("Task from nested launch")
}
delay(100L)
println("Task from coroutine scope") // This line will be printed before nested launch
}
println("Coroutine scope is over") // This line is not printed until nested launch completes
}
The output goes like so:
Task from coroutine scope
Task from runBlocking
Task from nested launch
Coroutine scope is over
My question is why this line:
println("Coroutine scope is over") // This line is not printed until nested launch completes
is called always last?
Shouldn't it be called since the:
coroutineScope { // Creates a new coroutine scope
....
}
is suspended?
There is also a note there:
The main difference between runBlocking and coroutineScope is that the latter does not block the current thread while waiting for all children to complete.
I dont understand how coroutineScope and runBlocking are different here? coroutineScope looks like its blocking since it only gets to the last line when it is done.
Can anyone enlighten me here?
I don't understand how coroutineScope and runBlocking are different here? coroutineScope looks like its blocking since it only gets to the last line when it is done.
There are two separate worlds: the suspendable world (within a coroutine) and the non-suspendable one. As soon as you enter the body of runBlocking, you are in the suspendable world, where suspend funs behave like blocking code and you can't get to the next line until the suspend fun returns. coroutineScope is a suspend fun that returns only when all the coroutines inside it are done. Therefore the last line must print at the end.
I copied the above explanation from a comment which seems to have clicked with readers. Here is the original answer:
From the perspective of the code in the block, your understanding is correct. The difference between runBlocking and coroutineScope happens at a lower level: what's happening to the thread while the coroutine is blocked?
runBlocking is not a suspend fun. The thread that called it remains inside it until the coroutine is complete.
coroutineScope is a suspend fun. If your coroutine suspends, the coroutineScope function gets suspended as well. This allows the top-level function, a non-suspending function that created the coroutine, to continue executing on the same thread. The thread has "escaped" the coroutineScope block and is ready to do some other work.
In your specific example: when your coroutineScope suspends, control returns to the implementation code inside runBlocking. This code is an event loop that drives all the coroutines you started within it. In your case, there will be some coroutines scheduled to run after a delay. When the time arrives, it will resume the appropriate coroutine, which will run for a short while, suspend, and then control will be again inside runBlocking.
While the above describes the conceptual similarities, it should also show you that runBlocking is a completely different tool from coroutineScope.
runBlocking is a low-level construct, to be used only in framework code or self-contained examples like yours. It turns an existing thread into an event loop and creates its coroutine with a Dispatcher that posts resuming coroutines to the event loop's queue.
coroutineScope is a user-facing construct, used to delineate the boundaries of a task that is being parallel-decomposed inside it. You use it to conveniently await on all the async work happening inside it, get the final result, and handle all failures at one central place.
The chosen answer is good but fails to address some other important aspects of the sample code that was provided. For instance, launch is non-blocking and is suppose to execute immediately. That is simply not true. The launch itself returns immediately BUT the code inside the launch does appear to be put into a queue and is only executed when any other launches that were previously put into the queue have completed.
Here's a similar piece of sample code with all the delays removed and an additional launch included. Without looking at the result below, see if you can predict the order in which the numbers are printed. Chances are that you will fail:
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
println("1")
}
coroutineScope {
launch {
println("2")
}
println("3")
}
coroutineScope {
launch {
println("4")
}
println("5")
}
launch {
println("6")
}
for (i in 7..100) {
println(i.toString())
}
println("101")
}
The result is:
3
1
2
5
4
7
8
9
10
...
99
100
101
6
The fact that number 6 is printed last, even after going through nearly 100 println have been executed, indicates that the code inside the last launch never gets executed until all non-blocking code after the launch has completed. But that is not really true either, because if that were the case, the first launch should not have executed until numbers 7 to 101 have completed. Bottom line? Mixing launch and coroutineScope is highly unpredictable and should be avoided if you expect a certain order in the way things should be executed.
To prove that code inside launches is placed into a queue and ONLY executed after ALL the non-blocking code has completed, run this (no coroutineScopes are used):
import kotlinx.coroutines.*
fun main() = runBlocking {
launch {
println("1")
}
launch {
println("2")
}
launch {
println("3")
}
for (i in 4..100) {
println(i.toString())
}
println("101")
}
This is the result you get:
4
5
6
...
101
1
2
3
Adding a CoroutineScope will break this behavior. It will cause all non-blocking code that follows the CoroutineScope to not be executed until ALL code prior to the CoroutineScope has completed.
It should also be noted that in this code sample, each of the launches in the queue are executed sequentially in the order that they are added to the queue and each launch will only execute AFTER the previous launch executes. This may make it appear that all launches share a common thread. This is not true. Each of them is given their own thread. However, if any code inside a launch calls a suspend function, the next launch in the queue is started immediately while the suspend function is being carried out. To be honest, this is very strange behavior. Why not just run all the launches in the queue asynchronously? While I don't know the internals of what goes on in this queue, my guess is that each launch in the queue does not get its own thread but all share a common thread. It is only when a suspend function is encountered does it appear that a new thread is created for the next launch in the queue. It may be done this way to save on resources.
To summarize, execution is done in this order:
Code inside a launch is placed inside a queue and are executed in the order that they are added.
Non-blocking code following a launch executes immediately before anything in the queue is executed.
A CoroutineScope blocks ALL code following it BUT will execute all the launch coroutines in the queue before resuming to the code following the CoroutineScope.
runBlocking is for you to block the main thread.
coroutineScope is for you to block the runBlocking.
Well, after having read all the answers here, I found none of them answered the question beyond repeating the wording of the fragments of the documentation.
So, I went on to search for an answer elsewhere and found it here. It practically shows the difference in behavior of coroutineScope and runBlocking (i.e. the difference between suspending and blocking)
runBlocking just blocks the current thread until inner coroutines will be completed. Here, thread that executes runBlocking will be blocked until the coroutine from coroutineScope will be finished.
First launch just won't allow the thread execute instructions that come after runBlocking, but will allow proceed to the instructions that come immediately after this launch block - that's why Task from coroutine scope is printed before than Task from runBlocking.
But nested coroutineScope in the context of runBlocking won't allow the thread to execute instructions that come after this coroutineScope block, because runBlocking will block the thread until the coroutine from coroutineScope will be finished completely. And that's why Coroutine scope is over will always come after Task from nested launch.
From this wonderful article https://jivimberg.io/blog/2018/05/04/parallel-map-in-kotlin/
suspend fun <A, B> Iterable<A>.pmap(f: suspend (A) -> B): List<B> = coroutineScope {
map { async { f(it) } }.awaitAll()
}
With runBlocking, we were not using Structured Concurrency, so an invocation of f could fail and all other executions would continue unfazed. And also we were not playing nice with the rest of the code. By using runBlocking we were forcefully blocking the thread until the whole execution of pmap finishes, instead of letting the caller decide how the execution should go.