marcinmoskala
10/18/2021, 3:42 PMasync
and launch
start a coroutine. runBlocking
is less clear, but I would say, that even though what is start is not concurrent, it starts a coroutine, it is just bound to the thread. But how about coroutineScope
? I think that they do not really start any new coroutine, just like suspending functions do not start a coroutine, but instead they can suspend the coroutine on which they are started. I just wanted to confirm if there are no disagreements here.Joffrey
10/18/2021, 4:03 PMrunBlocking
does start a coroutine - you will be able to run suspend functions inside it, and you were not able to outside of it. It just blocks the current thread while that coroutine executes. However, I wouldn't say the started coroutine is "bound to the current thread". You could do runBlocking(Dispatchers.Default)
and run your coroutine on a different thread.
This could actually be a major difference with coroutineScope
, which technically has to stay in the same context (and thus dispatcher).Erik
10/18/2021, 5:14 PMrunBlocking
coroutine is bound to the current thread, you could say that the current thread is bound to the coroutine. This is simply because the function doesn't return until the coroutine completes.marcinmoskala
10/18/2021, 5:38 PMrunBlocking(Dispatchers.Default)
, the body will run on the main thread. But the coroutine will still block the thread on which it is started until this body is finished. This is probably something that should be explained in the book 🤔marcinmoskala
10/18/2021, 5:39 PMimport kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
fun main() {
runBlocking(Dispatchers.Default) {
delay(1000)
println(Thread.currentThread().name)
}
runBlocking(Dispatchers.Default) {
delay(1000)
println(Thread.currentThread().name)
}
}
// (1 sec)
// DefaultDispatcher-worker-1
// (1 sec)
// DefaultDispatcher-worker-1
DALDEI
10/18/2021, 10:48 PMDALDEI
10/18/2021, 10:51 PMDALDEI
10/18/2021, 10:53 PMErik
10/19/2021, 7:12 AMrunBlocking
. What would have been a more logical name for you, indicating that the current thread will be blocked until the coroutine completes?Erik
10/19/2021, 7:13 AMrunWhileBlocking
? That has some connotations with while
which is completely different...Erik
10/19/2021, 7:13 AMsync {}
. It clearly communicates that something it synchronous, i.e. the block of coroutine code.Erik
10/19/2021, 7:15 AMrunBlocking
can only mean that it blocks until it returns a value. Admittedly, a function's name should be enough to understand what it does without looking at the signature. And it's less obvious if the return type is Unit
.julian
10/19/2021, 6:20 PMblockAndRun
?Joffrey
10/19/2021, 9:36 PMblockAndRun
could sound like block THEN run. Maybe blockToRun
?julian
10/20/2021, 1:47 AMjulian
10/20/2021, 1:48 AMblockToRun
too...Joffrey
10/20/2021, 6:24 AMJoffrey
10/20/2021, 6:25 AMrunBlocking
too 😂 I don't believe this will ever change anywayjulian
10/20/2021, 6:12 PMjulian
10/20/2021, 6:13 PMDALDEI
10/29/2021, 1:50 AMJoffrey
10/29/2021, 12:06 PMdelay()
it's most likely a mistake, and needs to be fixed. We could contribute to the docs in this respect. I don't quite share the opinion that the docs assume a coroutine-friendly environment, but I would need to re-read them to be sure. Any specific pointers about pieces of the docs you find confusing would be very helpful here.DALDEI
10/30/2021, 2:29 AMsuspend
*fun* doSomethingUsefulOne(): Int {
delay(1000L) // pretend we are doing something useful here
*return* 13
}
It is this exact example that a coworker (reasonably, IMHO) interpreted the above as equivilent to:
suspend fun doSomethingUsefulOne(): Int {
do-blocking-HTTP-call() // pretend we are doing something useful here
return 13
}
This 'works' well enough that you have to do more then a basic unit test to discover that it does not prevent the calling thread from blocking
I will speculate that one can classify ones experience in kotlin coroutines by the answer to the question:
is 'doSomethingUsefulOne' blocking or suspending ?
Then further refined by 'Exactly what did you mean by that'DALDEI
10/30/2021, 2:36 AM