https://kotlinlang.org logo
Title
c

chansek

04/16/2019, 10:33 AM
Where as in this case, the last line "Hello " executes first
o

octylFractal

04/16/2019, 10:34 AM
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

chansek

04/16/2019, 10:37 AM
In the first case also, delay(500L) is inside launch which is async. So why it prevents the last line to be executed?
o

octylFractal

04/16/2019, 10:38 AM
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

chansek

04/16/2019, 10:39 AM
Does that mean suspending and blocking is same?
Because, blocking context also prevent the last line to be executed
o

octylFractal

04/16/2019, 10:40 AM
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

chansek

04/16/2019, 10:44 AM
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

halim

04/18/2019, 12:01 PM
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")