@@ -23,7 +23,7 @@ class CancellationTimeOutsGuideTest {
23
23
* [ Cancelling coroutine execution] ( #cancelling-coroutine-execution )
24
24
* [ Cancellation is cooperative] ( #cancellation-is-cooperative )
25
25
* [ Making computation code cancellable] ( #making-computation-code-cancellable )
26
- * [ Closing resources with finally] ( #closing-resources-with-finally )
26
+ * [ Closing resources with ` finally ` ] ( #closing-resources-with-` finally` )
27
27
* [ Run non-cancellable block] ( #run-non-cancellable-block )
28
28
* [ Timeout] ( #timeout )
29
29
@@ -49,7 +49,7 @@ fun main() = runBlocking {
49
49
// sampleStart
50
50
val job = launch {
51
51
repeat(1000 ) { i ->
52
- println (" I'm sleeping $i ..." )
52
+ println (" job: I'm sleeping $i ..." )
53
53
delay(500L )
54
54
}
55
55
}
@@ -69,9 +69,9 @@ fun main() = runBlocking {
69
69
It produces the following output:
70
70
71
71
``` text
72
- I'm sleeping 0 ...
73
- I'm sleeping 1 ...
74
- I'm sleeping 2 ...
72
+ job: I'm sleeping 0 ...
73
+ job: I'm sleeping 1 ...
74
+ job: I'm sleeping 2 ...
75
75
main: I'm tired of waiting!
76
76
main: Now I can quit.
77
77
```
@@ -104,7 +104,7 @@ fun main() = runBlocking {
104
104
while (i < 5 ) { // computation loop, just wastes CPU
105
105
// print a message twice a second
106
106
if (System .currentTimeMillis() >= nextPrintTime) {
107
- println (" I'm sleeping ${i++ } ..." )
107
+ println (" job: I'm sleeping ${i++ } ..." )
108
108
nextPrintTime + = 500L
109
109
}
110
110
}
@@ -125,12 +125,12 @@ Run it to see that it continues to print "I'm sleeping" even after cancellation
125
125
until the job completes by itself after five iterations.
126
126
127
127
<!-- - TEST
128
- I'm sleeping 0 ...
129
- I'm sleeping 1 ...
130
- I'm sleeping 2 ...
128
+ job: I'm sleeping 0 ...
129
+ job: I'm sleeping 1 ...
130
+ job: I'm sleeping 2 ...
131
131
main: I'm tired of waiting!
132
- I'm sleeping 3 ...
133
- I'm sleeping 4 ...
132
+ job: I'm sleeping 3 ...
133
+ job: I'm sleeping 4 ...
134
134
main: Now I can quit.
135
135
-->
136
136
@@ -156,7 +156,7 @@ fun main() = runBlocking {
156
156
while (isActive) { // cancellable computation loop
157
157
// print a message twice a second
158
158
if (System .currentTimeMillis() >= nextPrintTime) {
159
- println (" I'm sleeping ${i++ } ..." )
159
+ println (" job: I'm sleeping ${i++ } ..." )
160
160
nextPrintTime + = 500L
161
161
}
162
162
}
@@ -173,22 +173,22 @@ fun main() = runBlocking {
173
173
174
174
> You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.kt ) .
175
175
176
- As you can see, now this loop is cancelled. [ isActive] is an extension property that is
177
- available inside the code of coroutine via [ CoroutineScope] object.
176
+ As you can see, now this loop is cancelled. [ isActive] is an extension property
177
+ available inside the coroutine via the [ CoroutineScope] object.
178
178
179
179
<!-- - TEST
180
- I'm sleeping 0 ...
181
- I'm sleeping 1 ...
182
- I'm sleeping 2 ...
180
+ job: I'm sleeping 0 ...
181
+ job: I'm sleeping 1 ...
182
+ job: I'm sleeping 2 ...
183
183
main: I'm tired of waiting!
184
184
main: Now I can quit.
185
185
-->
186
186
187
- ### Closing resources with finally
187
+ ### Closing resources with ` finally `
188
188
189
189
Cancellable suspending functions throw [ CancellationException] on cancellation which can be handled in
190
- a usual way. For example, ` try {...} finally {...} ` expression and Kotlin ` use ` function execute their
191
- finalization actions normally when coroutine is cancelled:
190
+ the usual way. For example, ` try {...} finally {...} ` expression and Kotlin ` use ` function execute their
191
+ finalization actions normally when a coroutine is cancelled:
192
192
193
193
194
194
<div class =" sample " markdown =" 1 " theme =" idea " data-min-compiler-version =" 1.3 " >
@@ -201,11 +201,11 @@ fun main() = runBlocking {
201
201
val job = launch {
202
202
try {
203
203
repeat(1000 ) { i ->
204
- println (" I'm sleeping $i ..." )
204
+ println (" job: I'm sleeping $i ..." )
205
205
delay(500L )
206
206
}
207
207
} finally {
208
- println (" I'm running finally" )
208
+ println (" job: I'm running finally" )
209
209
}
210
210
}
211
211
delay(1300L ) // delay a bit
@@ -220,15 +220,15 @@ fun main() = runBlocking {
220
220
221
221
> You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-cancel-04.kt ) .
222
222
223
- Both [ join] [ Job.join ] and [ cancelAndJoin] wait for all the finalization actions to complete,
223
+ Both [ join] [ Job.join ] and [ cancelAndJoin] wait for all finalization actions to complete,
224
224
so the example above produces the following output:
225
225
226
226
``` text
227
- I'm sleeping 0 ...
228
- I'm sleeping 1 ...
229
- I'm sleeping 2 ...
227
+ job: I'm sleeping 0 ...
228
+ job: I'm sleeping 1 ...
229
+ job: I'm sleeping 2 ...
230
230
main: I'm tired of waiting!
231
- I'm running finally
231
+ job: I'm running finally
232
232
main: Now I can quit.
233
233
```
234
234
@@ -240,7 +240,7 @@ Any attempt to use a suspending function in the `finally` block of the previous
240
240
[ CancellationException] , because the coroutine running this code is cancelled. Usually, this is not a
241
241
problem, since all well-behaving closing operations (closing a file, cancelling a job, or closing any kind of a
242
242
communication channel) are usually non-blocking and do not involve any suspending functions. However, in the
243
- rare case when you need to suspend in the cancelled coroutine you can wrap the corresponding code in
243
+ rare case when you need to suspend in a cancelled coroutine you can wrap the corresponding code in
244
244
` withContext(NonCancellable) {...} ` using [ withContext] function and [ NonCancellable] context as the following example shows:
245
245
246
246
<div class =" sample " markdown =" 1 " theme =" idea " data-min-compiler-version =" 1.3 " >
@@ -253,14 +253,14 @@ fun main() = runBlocking {
253
253
val job = launch {
254
254
try {
255
255
repeat(1000 ) { i ->
256
- println (" I'm sleeping $i ..." )
256
+ println (" job: I'm sleeping $i ..." )
257
257
delay(500L )
258
258
}
259
259
} finally {
260
260
withContext(NonCancellable ) {
261
- println (" I'm running finally" )
261
+ println (" job: I'm running finally" )
262
262
delay(1000L )
263
- println (" And I've just delayed for 1 sec because I'm non-cancellable" )
263
+ println (" job: And I've just delayed for 1 sec because I'm non-cancellable" )
264
264
}
265
265
}
266
266
}
@@ -277,18 +277,18 @@ fun main() = runBlocking {
277
277
> You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-cancel-05.kt ) .
278
278
279
279
<!-- - TEST
280
- I'm sleeping 0 ...
281
- I'm sleeping 1 ...
282
- I'm sleeping 2 ...
280
+ job: I'm sleeping 0 ...
281
+ job: I'm sleeping 1 ...
282
+ job: I'm sleeping 2 ...
283
283
main: I'm tired of waiting!
284
- I'm running finally
285
- And I've just delayed for 1 sec because I'm non-cancellable
284
+ job: I'm running finally
285
+ job: And I've just delayed for 1 sec because I'm non-cancellable
286
286
main: Now I can quit.
287
287
-->
288
288
289
289
### Timeout
290
290
291
- The most obvious reason to cancel coroutine execution in practice
291
+ The most obvious practical reason to cancel execution of a coroutine
292
292
is because its execution time has exceeded some timeout.
293
293
While you can manually track the reference to the corresponding [ Job] and launch a separate coroutine to cancel
294
294
the tracked one after delay, there is a ready to use [ withTimeout] function that does it.
@@ -331,10 +331,10 @@ We have not seen its stack trace printed on the console before. That is because
331
331
inside a cancelled coroutine ` CancellationException ` is considered to be a normal reason for coroutine completion.
332
332
However, in this example we have used ` withTimeout ` right inside the ` main ` function.
333
333
334
- Because cancellation is just an exception, all the resources are closed in a usual way.
335
- You can wrap the code with timeout in ` try {...} catch (e: TimeoutCancellationException) {...} ` block if
336
- you need to do some additional action specifically on any kind of timeout or use [ withTimeoutOrNull] function
337
- that is similar to [ withTimeout] , but returns ` null ` on timeout instead of throwing an exception:
334
+ Since cancellation is just an exception, all resources are closed in the usual way.
335
+ You can wrap the code with timeout in a ` try {...} catch (e: TimeoutCancellationException) {...} ` block if
336
+ you need to do some additional action specifically on any kind of timeout or use the [ withTimeoutOrNull] function
337
+ that is similar to [ withTimeout] but returns ` null ` on timeout instead of throwing an exception:
338
338
339
339
<div class =" sample " markdown =" 1 " theme =" idea " data-min-compiler-version =" 1.3 " >
340
340
0 commit comments