@@ -65,9 +65,8 @@ context of the main `runBlocking` coroutine which runs in the `main` thread.
65
65
[ Dispatchers.Unconfined] is a special dispatcher that also appears to run in the ` main ` thread, but it is,
66
66
in fact, a different mechanism that is explained later.
67
67
68
- The default dispatcher that is used when coroutines are launched in [ GlobalScope]
69
- is represented by [ Dispatchers.Default] and uses a shared background pool of threads,
70
- so ` launch(Dispatchers.Default) { ... } ` uses the same dispatcher as ` GlobalScope.launch { ... } ` .
68
+ The default dispatcher that is used when no other dispatcher is explicitly specified in the scope.
69
+ It is represented by [ Dispatchers.Default] and uses a shared background pool of threads.
71
70
72
71
[ newSingleThreadContext] creates a thread for the coroutine to run.
73
72
A dedicated thread is a very expensive resource.
@@ -303,8 +302,14 @@ the [Job] of the new coroutine becomes
303
302
a _ child_ of the parent coroutine's job. When the parent coroutine is cancelled, all its children
304
303
are recursively cancelled, too.
305
304
306
- However, when [ GlobalScope] is used to launch a coroutine, there is no parent for the job of the new coroutine.
307
- It is therefore not tied to the scope it was launched from and operates independently.
305
+ However, this parent-child relation can be explicitly overriden in one of two ways:
306
+
307
+ 1 . When a different scope is explicitly specified when launching a coroutine (for example, ` GlobalScope.launch ` ),
308
+ then it does not inherit a ` Job ` from the parent scope.
309
+ 2 . When a different ` Job ` object is passed as the context for the new coroutine (as show in the example below),
310
+ then it overrides the ` Job ` of the parent scope.
311
+
312
+ In both cases, the launched coroutine is not tied to the scope it was launched from and operates independently.
308
313
309
314
``` kotlin
310
315
import kotlinx.coroutines.*
@@ -313,9 +318,9 @@ fun main() = runBlocking<Unit> {
313
318
// sampleStart
314
319
// launch a coroutine to process some kind of incoming request
315
320
val request = launch {
316
- // it spawns two other jobs, one with GlobalScope
317
- GlobalScope . launch {
318
- println (" job1: I run in GlobalScope and execute independently!" )
321
+ // it spawns two other jobs
322
+ launch( Job ()) {
323
+ println (" job1: I run in my own Job and execute independently!" )
319
324
delay(1000 )
320
325
println (" job1: I am not affected by cancellation of the request" )
321
326
}
@@ -343,7 +348,7 @@ fun main() = runBlocking<Unit> {
343
348
The output of this code is:
344
349
345
350
``` text
346
- job1: I run in GlobalScope and execute independently!
351
+ job1: I run in my own Job and execute independently!
347
352
job2: I am a child of the request coroutine
348
353
job1: I am not affected by cancellation of the request
349
354
main: Who has survived request cancellation?
@@ -659,7 +664,6 @@ that should be implemented.
659
664
[ async ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html
660
665
[ CoroutineScope ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html
661
666
[ Dispatchers.Unconfined ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-unconfined.html
662
- [ GlobalScope ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-global-scope/index.html
663
667
[ Dispatchers.Default ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html
664
668
[ newSingleThreadContext ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/new-single-thread-context.html
665
669
[ ExecutorCoroutineDispatcher.close ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-executor-coroutine-dispatcher/close.html
@@ -678,4 +682,4 @@ that should be implemented.
678
682
[ ensurePresent ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/java.lang.-thread-local/ensure-present.html
679
683
[ ThreadContextElement ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-thread-context-element/index.html
680
684
681
- <!-- - END -->
685
+ <!-- - END -->
0 commit comments