Skip to content

Commit e303e6b

Browse files
committed
Clarify difference between runBlocking and coroutineScope
Fixes #1439
1 parent b7b5adb commit e303e6b

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

docs/basics.md

+11-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,14 @@ World!
251251
### Scope builder
252252
In addition to the coroutine scope provided by different builders, it is possible to declare your own scope using
253253
[coroutineScope] builder. It creates a coroutine scope and does not complete until all launched children
254-
complete. The main difference between [runBlocking] and [coroutineScope] is that the latter does not block the current thread
255-
while waiting for all children to complete.
254+
complete.
255+
256+
[runBlocking] and [coroutineScope] may look similar because they both wait for its body and all its children to complete.
257+
The main difference between these two is that the [runBlocking] method _blocks_ the current thread for waiting,
258+
while [coroutineScope] just suspends, releasing the underlying thread for other usages.
259+
Because of that difference, [runBlocking] is a regular function and [coroutineScope] is a suspending function.
260+
261+
It can be demonstrated by the following example:
256262

257263
<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
258264

@@ -290,6 +296,9 @@ Task from nested launch
290296
Coroutine scope is over
291297
-->
292298

299+
Note that right after "Task from coroutine scope" message, while waiting for nested launch,
300+
"Task from runBlocking" is executed and printed, though coroutineScope is not completed yet.
301+
293302
### Extract function refactoring
294303

295304
Let's extract the block of code inside `launch { ... }` into a separate function. When you

0 commit comments

Comments
 (0)