File tree 1 file changed +11
-2
lines changed
1 file changed +11
-2
lines changed Original file line number Diff line number Diff line change @@ -251,8 +251,14 @@ World!
251
251
### Scope builder
252
252
In addition to the coroutine scope provided by different builders, it is possible to declare your own scope using
253
253
[ 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:
256
262
257
263
<div class =" sample " markdown =" 1 " theme =" idea " data-min-compiler-version =" 1.3 " >
258
264
@@ -290,6 +296,9 @@ Task from nested launch
290
296
Coroutine scope is over
291
297
-->
292
298
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
+
293
302
### Extract function refactoring
294
303
295
304
Let's extract the block of code inside ` launch { ... } ` into a separate function. When you
You can’t perform that action at this time.
0 commit comments