@@ -7,14 +7,16 @@ import kotlin.coroutines.*
7
7
8
8
/* *
9
9
* Launches new coroutine without blocking current thread and returns a reference to the coroutine as a [Job].
10
- * The [context] for the new coroutine must be explicitly specified and must include [CoroutineDispatcher] element.
10
+ * The running coroutine is cancelled when the resulting job is [cancelled][Job.cancel].
11
+ *
12
+ * The [context] for the new coroutine must be explicitly specified.
11
13
* See [CoroutineDispatcher] for the standard [context] implementations that are provided by `kotlinx.coroutines`.
12
- * The specified context is added to the context of the parent running coroutine (if any) inside which this function
13
- * is invoked. The [Job] of the resulting coroutine is a child of the job of the parent coroutine (if any) .
14
+ * The [ context][CoroutineScope. context] of the parent coroutine from its [scope][CoroutineScope] may be used,
15
+ * in which case the [Job] of the resulting coroutine is a child of the job of the parent coroutine.
14
16
*
15
17
* Uncaught exceptions in this coroutine cancel parent job in the context by default
16
- * (unless [CoroutineExceptionHandler] is explicitly specified), which means that when `launch` is used from another
17
- * coroutine, any uncaught exception leads to the cancellation of parent coroutine.
18
+ * (unless [CoroutineExceptionHandler] is explicitly specified), which means that when `launch` is used with
19
+ * the context of another coroutine, then any uncaught exception leads to the cancellation of parent coroutine.
18
20
*
19
21
* See [newCoroutineContext] for a description of debugging facilities that are available for newly created coroutine.
20
22
*/
@@ -23,9 +25,11 @@ fun launch(context: CoroutineContext, block: suspend CoroutineScope.() -> Unit):
23
25
24
26
/* *
25
27
* Calls the specified suspending block with a given coroutine context, suspends until it completes, and returns
26
- * the result. It immediately applies dispatcher from the new context, shifting execution of the block into the
28
+ * the result.
29
+ *
30
+ * This function immediately applies dispatcher from the new context, shifting execution of the block into the
27
31
* different thread inside the block, and back when it completes.
28
- * The specified [context] is merged onto the current coroutine context.
32
+ * The specified [context] is added onto the current coroutine context for the execution of the block .
29
33
*/
30
34
public suspend fun <T > run (context : CoroutineContext , block : suspend CoroutineScope .() -> T ): T =
31
35
suspendCoroutine { cont ->
@@ -41,8 +45,6 @@ public suspend fun <T> run(context: CoroutineContext, block: suspend CoroutineSc
41
45
* The default [CoroutineDispatcher] for this builder in an implementation of [EventLoop] that processes continuations
42
46
* in this blocked thread until the completion of this coroutine.
43
47
* See [CoroutineDispatcher] for the other implementations that are provided by `kotlinx.coroutines`.
44
- * The specified [context] is added to the context of the parent running coroutine (if any) inside which this function
45
- * is invoked. The [Job] of the resulting coroutine is a child of the job of the parent coroutine (if any).
46
48
*
47
49
* If this blocked thread is interrupted (see [Thread.interrupt]), then the coroutine job is cancelled and
48
50
* this `runBlocking` invocation throws [InterruptedException].
@@ -52,7 +54,7 @@ public suspend fun <T> run(context: CoroutineContext, block: suspend CoroutineSc
52
54
@Throws(InterruptedException ::class )
53
55
public fun <T > runBlocking (context : CoroutineContext = EmptyCoroutineContext , block : suspend CoroutineScope .() -> T ): T {
54
56
val currentThread = Thread .currentThread()
55
- val privateEventLoop = if (context[ContinuationInterceptor ] as ? CoroutineDispatcher == null )
57
+ val privateEventLoop = if (context[ContinuationInterceptor ] == null )
56
58
EventLoopImpl (currentThread) else null
57
59
val newContext = newCoroutineContext(context + (privateEventLoop ? : EmptyCoroutineContext ))
58
60
val coroutine = BlockingCoroutine <T >(newContext, currentThread, privateEventLoop != null )
0 commit comments