Skip to content

Commit bba968d

Browse files
将“暂停函数”更改为“挂起函数”
1 parent 8167f39 commit bba968d

File tree

2 files changed

+25
-25
lines changed

2 files changed

+25
-25
lines changed

docs/basics.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ World!
8383
Error: Kotlin: Suspend functions are only allowed to be called from a coroutine or another suspend function
8484
```
8585

86-
这是因为 [delay] 是一个特别的 _暂停函数_ ,它不会造成线程阻塞,但是 _暂停_<!--
86+
这是因为 [delay] 是一个特别的 _挂起函数_它不会造成线程阻塞,但是 _挂起_<!--
8787
-->函数只能在协程中使用.
8888

8989
### 桥接阻塞和非阻塞的世界
@@ -151,7 +151,7 @@ World!
151151
这里的 `runBlocking<Unit> { ... }` 作为一个适配器被用来启动最高优先级的主协程。
152152
我们明确的声明 `Unit` 为返回值类型,因为Kotlin中的`main`函数返回`Unit`类型。
153153

154-
这也是一种使用暂停函数来实现单元测试的方法
154+
这也是一种使用挂起函数来实现单元测试的方法
155155

156156
<!--- INCLUDE
157157
import kotlinx.coroutines.*
@@ -163,7 +163,7 @@ import kotlinx.coroutines.*
163163
class MyTest {
164164
@Test
165165
fun testMySuspendingFunction() = runBlocking<Unit> {
166-
// 这里我们可以使用暂停函数来实现任何我们喜欢的断言风格
166+
// 这里我们可以使用挂起函数来实现任何我们喜欢的断言风格
167167
}
168168
}
169169
```
@@ -294,8 +294,8 @@ Coroutine scope is over
294294

295295
让我们在 `launch { ... }` 中提取代码块并分离到另一个函数中. 当你
296296
在这段代码上展示"提取函数"函数的时候, 你的到了一个新的函数并用 `suspend` 修饰.
297-
这是你的第一个 _暂停函数_ 。暂停函数可以像一个普通的函数一样使用内部协程,但是它们拥有一些额外的特性,反过来说,
298-
使用其它的暂停函数, 比如这个例子中的 `delay`,可以使协程暂停执行。
297+
这是你的第一个 _挂起函数_ 。挂起函数可以像一个普通的函数一样使用内部协程,但是它们拥有一些额外的特性,反过来说,
298+
使用其它的挂起函数, 比如这个例子中的 `delay`,可以使协程暂停执行。
299299

300300
<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
301301

@@ -307,7 +307,7 @@ fun main() = runBlocking {
307307
println("Hello,")
308308
}
309309

310-
// 你的第一个暂停函数
310+
// 你的第一个挂起函数
311311
suspend fun doWorld() {
312312
delay(1000L)
313313
println("World!")

docs/cancellation-and-timeouts.md

+19-19
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class CancellationTimeOutsGuideTest {
4646
import kotlinx.coroutines.*
4747

4848
fun main() = runBlocking {
49-
//示例开始
49+
//sampleStart
5050
val job = launch {
5151
repeat(1000) { i ->
5252
println("I'm sleeping $i ...")
@@ -58,7 +58,7 @@ fun main() = runBlocking {
5858
job.cancel() // 取消该任务
5959
job.join() // 等待任务执行结束
6060
println("main: Now I can quit.")
61-
//示例结束
61+
//sampleEnd
6262
}
6363
```
6464

@@ -85,7 +85,7 @@ main: Now I can quit.
8585
### 取消协作
8686

8787
协程的取消是 _协作_ 的。一段协程代码必须合作才能被取消。
88-
所有 `kotlinx.coroutines` 中的暂停函数都是 _可被取消的_ 。它们检查协程的取消,
88+
所有 `kotlinx.coroutines` 中的挂起函数都是 _可被取消的_ 。它们检查协程的取消,
8989
并在取消时抛出 [CancellationException]。 然而,如果协程正在执行<!--
9090
-->计算任务,并且没有检查取消的话,那么它是不能被取消的,就如如下示例<!--
9191
-->代码所示:
@@ -96,7 +96,7 @@ main: Now I can quit.
9696
import kotlinx.coroutines.*
9797

9898
fun main() = runBlocking {
99-
//示例开始
99+
//sampleStart
100100
val startTime = System.currentTimeMillis()
101101
val job = launch(Dispatchers.Default) {
102102
var nextPrintTime = startTime
@@ -113,7 +113,7 @@ fun main() = runBlocking {
113113
println("main: I'm tired of waiting!")
114114
job.cancelAndJoin() // 取消一个任务并且等待它结束
115115
println("main: Now I can quit.")
116-
//示例结束
116+
//sampleEnd
117117
}
118118
```
119119

@@ -137,7 +137,7 @@ main: Now I can quit.
137137
### 使执行计算的代码可被取消
138138

139139
我们有两种方法来使执行计算的代码可以被取消。第一种方法是定期<!--
140-
-->调用暂停函数来检查取消。 对于这种目的 [yield] 是一个好的选择。
140+
-->调用挂起函数来检查取消。 对于这种目的 [yield] 是一个好的选择。
141141
另一种方法是明确的检查取消状态。让我们试试第二种方法。
142142

143143
将前一个示例中的 `while (i < 5)` 替换为 `while (isActive)` 并重新运行它。
@@ -148,7 +148,7 @@ main: Now I can quit.
148148
import kotlinx.coroutines.*
149149

150150
fun main() = runBlocking {
151-
//示例开始
151+
//sampleStart
152152
val startTime = System.currentTimeMillis()
153153
val job = launch(Dispatchers.Default) {
154154
var nextPrintTime = startTime
@@ -165,7 +165,7 @@ fun main() = runBlocking {
165165
println("main: I'm tired of waiting!")
166166
job.cancelAndJoin() // 取消该任务并等待它结束
167167
println("main: Now I can quit.")
168-
//示例结束
168+
//sampleEnd
169169
}
170170
```
171171

@@ -187,7 +187,7 @@ main: Now I can quit.
187187
### 在finally中释放资源
188188

189189
我们通常使用如下的方法处理在被取消时抛出 [CancellationException] 的可被取消<!--
190-
-->的暂停函数。比如说,`try {...} finally {...}` 表达式以及 Kotlin 的 `use` 函数一般在协程被取消的时候<!--
190+
-->的挂起函数。比如说,`try {...} finally {...}` 表达式以及 Kotlin 的 `use` 函数一般在协程被取消的时候<!--
191191
-->执行它们的终结动作:
192192

193193

@@ -197,7 +197,7 @@ main: Now I can quit.
197197
import kotlinx.coroutines.*
198198

199199
fun main() = runBlocking {
200-
//示例开始
200+
//sampleStart
201201
val job = launch {
202202
try {
203203
repeat(1000) { i ->
@@ -212,7 +212,7 @@ fun main() = runBlocking {
212212
println("main: I'm tired of waiting!")
213213
job.cancelAndJoin() // 取消该任务并且等待它结束
214214
println("main: Now I can quit.")
215-
//示例结束
215+
//sampleEnd
216216
}
217217
```
218218

@@ -236,10 +236,10 @@ main: Now I can quit.
236236

237237
### 运行不能被取消的代码块
238238

239-
在前一个例子中任何尝试在 `finally` 块中调用暂停函数的行为都会抛出
239+
在前一个例子中任何尝试在 `finally` 块中调用挂起函数的行为都会抛出
240240
[CancellationException],因为这里持续运行的代码是可以被取消的。通常,这并不是一个<!--
241241
-->问题,所有良好的关闭操作(关闭一个文件、取消一个任务,或是关闭任何一种<!--
242-
-->通信通道)通常都是非阻塞的,并且不会调用任何暂停函数。然而,在<!--
242+
-->通信通道)通常都是非阻塞的,并且不会调用任何挂起函数。然而,在<!--
243243
-->真实的案例中,当你需要挂起一个被取消的协程,你可以将相应的代码包装在
244244
`withContext(NonCancellable) {...}` 中,并使用 [withContext] 函数以及 [NonCancellable] 上下文,见如下示例所示:
245245

@@ -249,7 +249,7 @@ main: Now I can quit.
249249
import kotlinx.coroutines.*
250250

251251
fun main() = runBlocking {
252-
//示例开始
252+
//sampleStart
253253
val job = launch {
254254
try {
255255
repeat(1000) { i ->
@@ -268,7 +268,7 @@ fun main() = runBlocking {
268268
println("main: I'm tired of waiting!")
269269
job.cancelAndJoin() // 取消该任务并等待它结束
270270
println("main: Now I can quit.")
271-
//示例结束
271+
//sampleEnd
272272
}
273273
```
274274

@@ -300,14 +300,14 @@ main: Now I can quit.
300300
import kotlinx.coroutines.*
301301

302302
fun main() = runBlocking {
303-
//示例开始
303+
//sampleStart
304304
withTimeout(1300L) {
305305
repeat(1000) { i ->
306306
println("I'm sleeping $i ...")
307307
delay(500L)
308308
}
309309
}
310-
//示例结束
310+
//sampleEnd
311311
}
312312
```
313313

@@ -342,7 +342,7 @@ Exception in thread "main" kotlinx.coroutines.TimeoutCancellationException: Time
342342
import kotlinx.coroutines.*
343343

344344
fun main() = runBlocking {
345-
//示例开始
345+
//sampleStart
346346
val result = withTimeoutOrNull(1300L) {
347347
repeat(1000) { i ->
348348
println("I'm sleeping $i ...")
@@ -351,7 +351,7 @@ fun main() = runBlocking {
351351
"Done" // 在它运行得到结果之前取消它
352352
}
353353
println("Result is $result")
354-
//示例结束
354+
//sampleEnd
355355
}
356356
```
357357

0 commit comments

Comments
 (0)