@@ -65,9 +65,8 @@ context of the main `runBlocking` coroutine which runs in the `main` thread.
6565[ Dispatchers.Unconfined] is a special dispatcher that also appears to run in the ` main ` thread, but it is,
6666in fact, a different mechanism that is explained later.
6767
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.
7170
7271[ newSingleThreadContext] creates a thread for the coroutine to run.
7372A dedicated thread is a very expensive resource.
@@ -303,8 +302,14 @@ the [Job] of the new coroutine becomes
303302a _ child_ of the parent coroutine's job. When the parent coroutine is cancelled, all its children
304303are recursively cancelled, too.
305304
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.
308313
309314``` kotlin
310315import kotlinx.coroutines.*
@@ -313,9 +318,9 @@ fun main() = runBlocking<Unit> {
313318// sampleStart
314319 // launch a coroutine to process some kind of incoming request
315320 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!" )
319324 delay(1000 )
320325 println (" job1: I am not affected by cancellation of the request" )
321326 }
@@ -343,7 +348,7 @@ fun main() = runBlocking<Unit> {
343348The output of this code is:
344349
345350``` text
346- job1: I run in GlobalScope and execute independently!
351+ job1: I run in my own Job and execute independently!
347352job2: I am a child of the request coroutine
348353job1: I am not affected by cancellation of the request
349354main: Who has survived request cancellation?
@@ -659,7 +664,6 @@ that should be implemented.
659664[ async ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/async.html
660665[ CoroutineScope ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html
661666[ 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
663667[ Dispatchers.Default ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-dispatchers/-default.html
664668[ newSingleThreadContext ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/new-single-thread-context.html
665669[ 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.
678682[ ensurePresent ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/java.lang.-thread-local/ensure-present.html
679683[ ThreadContextElement ] : https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-thread-context-element/index.html
680684
681- <!-- - END -->
685+ <!-- - END -->
0 commit comments