Skip to content

Commit d072ae8

Browse files
committed
docs(coroutine): add explanation for CoroutineContext and its elements
- Describe the parameters of launch/async methods for creating coroutines - Explain the structure and elements of CoroutineContext - Detail the purpose of each CoroutineContext element: - Job: coroutine handle/unique identifier - CoroutineDispatcher: specifies the thread for coroutine execution - CoroutineName: assigns a name to the coroutine for debugging - CoroutineExceptionHandler: specifies an exception handler for uncaught exceptions
1 parent 26a1cce commit d072ae8

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.sinlov.kotlin.playground.coroutine
2+
3+
import io.kotest.core.spec.style.FunSpec
4+
import kotlinx.coroutines.launch
5+
import kotlinx.coroutines.async
6+
7+
class CoroutinesMust : FunSpec({
8+
test("CoroutineContext") {
9+
/**
10+
* The method [launch]/[async] for creating coroutines has a total of 3 parameters,
11+
* with the last one being the coroutine code block,
12+
* and the other two parameters are used to control the coroutine.
13+
*/
14+
15+
/**
16+
* [kotlin.coroutines.CoroutineContext] is a collection, the specific element type is [kotlin.coroutines.CoroutineContext.Element]
17+
* Specific elements have four types.
18+
* [kotlinx.coroutines.Job]:协程的句柄,或者说唯一标识,用以具体控制每个协程的(cancel和join等),具有树形关系
19+
* [kotlinx.coroutines.CoroutineDispatcher]:用以指定协程的运行线程
20+
* [kotlinx.coroutines.CoroutineName]:给协程取个名字,方便调试
21+
* [kotlinx.coroutines.CoroutineExceptionHandler]:指定协程的异常处理器,用以处理未被捕获的异常
22+
*/
23+
}
24+
})

0 commit comments

Comments
 (0)