hi, i have some questions about kotlin coroutines ...
# coroutines
h
hi, i have some questions about kotlin coroutines 1. "suspend fucntion can suspend coroutine but not block a thread", so when coroutine is suspend who is the thread responsible to execute this suspend function ?? 2. what diff between corountineContext.cancel() vs job.cancel() 3. what diff between CoroutineScope.xxx() vs suspend fun xxx()
d
3) By convention,
<http://CoroutineScope.xxx|CoroutineScope.xxx>()
spawns coroutines with the given scope with run even after the function returns.
suspend fun xxx()
finishes all it's work before returning.
1) a
suspend
function does not block the current thread. So it might block some other dedicated thread for blocking or it might rely on the OS calling
resume
.
If a function that takes a callback does not block the caller, who is the thread responsible to execute this callback function?
h
thanks for response but is not clear 100% can you explain whats happen with this simple suspend sum function: launch { suspend sum() = (1..100).sum() } this function run in main-thread son when launch coroutne suspend sum continue exection in thread-??? how can sum() be resumed in main-thread ?
d
If you use
launch
it'll run the code in a background thread pool.
h
pool thread you mean an other thread ?? but if i log in sum() it still running in main thread
d
Hmm, can you use a proper code block, I don't understand what you've posted.
h
Copy code
suspend sum() {
   println("$Thread.currentThread().name")
   (1..VAL).sum() 
}   
fun main() = runBlocking() {
   lunch {
      sum() 
  }
   println("end")
}
the program still run sequantielly and sum() still run in main-thread, so suspend here what deans if suspend function run in same thread of caller
u
Which platform? On jvm there is the default dispatcher, using a thread pool for all coroutines launched to it. JS and native have no threading support. So they dispatch to the main (and only) thread.
t
Coroutines wrapped in
runBlocking
are executed sequentially, blocking the current thread instead of suspending (hence the name). So your code is sequential and runs on the main thread only.
1.
Dispatchers
are context elements that you pass to
launch
or
async
that define the threading policy. See https://kotlinlang.org/docs/reference/coroutines/coroutine-context-and-dispatchers.html#dispatchers-and-threads 2.
Job.cancel()
and
CoroutineContext.cancel()
does the same thing: cancelling the Job and their children's. 3. Dominic explained it pretty well.
u
Use
launch(Dispatcher.Default)
to schedule to background
n
from javadoc
launch
Copy code
If the context does not have any dispatcher nor any other [ContinuationInterceptor], then [Dispatchers.Default] is used.
runblocking
by default creates context with current thread as dispatcher, so
launch
by default will be in main thread in that example
h
hi devs, in kotlin coroutine "suspend functon will suspend coroutone and not block the thread" so when sum() method in execute it will be suspend coroutine (lunch) and the main-thread i not block and continue to execute rest of code, so sum () method in wich thread execute ??
u
As mentioned above, it depends on the dispatcher. You can pass a dispatcher to `launch`like this:
Copy code
suspend sum() {
   println("$Thread.currentThread().name")
   (1..VAL).sum() 
}   
fun main() = runBlocking() {
   launch(Dispatchers.Default) {
      sum() 
  }
   println("end")
}
And btw.
end
might be printed before
sum
is even started. If you want to wait for execution of `sum`go like this:
Copy code
suspend sum() {
   println("$Thread.currentThread().name")
   (1..VAL).sum() 
}   
fun main() = runBlocking() {
   withContext(Dispatchers.Default) {
      sum() 
  }
   println("end")
}
h
no i wan't to spacify any dispatcher but i just want to know about how is responsable and how exeucte a suspend funtion is running when coroutine is suspended
if the coroutine is suspended so the thread is free to execute other task (not suspend function) but how about suspend function ?? how is the delagate of execution ??
"The thread is returned to the pool while the coroutine is waiting, and when the waiting is done, the coroutine resumes on a free thread in the pool" does mean when coroutine is waiting for suspend function to complete the thead is free but the suspend how continue here execution ???
d
Hmm, I had similar confusion when I was first learning suspend functions. Given the answers to your questions are mostly "it depends", I think it'll be best if you read the informal docs as it will put you in the right frame of mind to ask/answer your questions.
h
@Dominaezzz thanks, you say that you have the same confusion like me so whats your conclusion ??
d
My conclusion was
suspend
functions are just sugar for functions with callbacks. Any question I had about coroutines or suspending stuff, I simply restructured the question to say callback instead of suspend and I had my answer.
Also comparing stuff with futures and go routines helped a lot.
h
@Dominaezzz thanks for your time