Where as in this case, the last line "Hello " exec...
# coroutines
c
Where as in this case, the last line "Hello " executes first
o
launch
is async, so while in the first case the
delay(500L)
inside
coroutineScope
prevented the last line from executing until after
launch
had finished, it doesn't do that in this case
👍 1
c
In the first case also, delay(500L) is inside launch which is async. So why it prevents the last line to be executed?
o
coroutineScope
suspends until all inside coroutines are complete, including `launch`es
ref
coroutineScope
docs:
This function returns as soon as given block and all its children coroutines are completed.
c
Does that mean suspending and blocking is same?
Because, blocking context also prevent the last line to be executed
o
no, they are different -- suspending won't take up a thread, blocking will
if you add some more prints, you can notice that the
launch
in the
coroutineScope
will actually run async to the
delay(100L)
, despite
runBlocking
only having one thread
if
delay
blocked, it wouldn't run until after the
println("... from coroutine scope")
c
Looks like, I am understanding a bit now. Thanks a lot for the help. I will explore a bit more. Hope you wouldn't mind if I have further basic doubts like this
👍 1
h
launch is coroutine build of type Job so its return immedatly (like future of comptetebalefuture) so the main is pass the second instruction (println("hello")) and not wait for launch to complete, also launch is delay fo 1 sec and this is an other reason the print ("hello") on first, delay is function that execute is separate thread (with executor named "scheduler") so the main-thread will be free to continue execution (not println("world")) because launch in this case is suspended (blocking) until delay finish) so main-thread pass to rest of your code (println("hello")), when delay finish its resume in main-thread (context of resume by default is the context of caller and in this case is main-thread you can change it if you implement continuation interface) and println("world")