You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-01.kt).
35
+
<!--- KNIT example-cancel-01.kt -->
36
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-cancel-01.kt).
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-02.kt).
89
+
<!--- KNIT example-cancel-02.kt -->
90
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-cancel-02.kt).
90
91
>
91
-
{type="note"}
92
+
{style="note"}
92
93
93
94
Run it to see that it continues to print "I'm sleeping" even after cancellation
94
95
until the job completes by itself after five iterations.
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.kt).
134
+
<!--- KNIT example-cancel-03.kt -->
135
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.kt).
135
136
>
136
-
{type="note"}
137
+
{style="note"}
137
138
138
139
While catching `Exception` is an anti-pattern, this issue may surface in more subtle ways, like when using the
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-04.kt).
176
+
<!--- KNIT example-cancel-04.kt -->
177
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-cancel-04.kt).
177
178
>
178
-
{type="note"}
179
+
{style="note"}
179
180
180
181
As you can see, now this loop is cancelled. [isActive] is an extension property
181
182
available inside the coroutine via the [CoroutineScope] object.
@@ -188,7 +189,7 @@ main: I'm tired of waiting!
188
189
main: Now I can quit.
189
190
-->
190
191
191
-
## Closing resources with `finally`
192
+
## Closing resources with finally
192
193
193
194
Cancellable suspending functions throw [CancellationException] on cancellation, which can be handled in
194
195
the usual way. For example, the `try {...} finally {...}` expression and Kotlin's `use` function execute their
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-05.kt).
221
+
<!--- KNIT example-cancel-05.kt -->
222
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-cancel-05.kt).
222
223
>
223
-
{type="note"}
224
+
{style="note"}
224
225
225
226
Both [join][Job.join] and [cancelAndJoin] wait for all finalization actions to complete,
226
227
so the example above produces the following output:
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-06.kt).
276
+
<!--- KNIT example-cancel-06.kt -->
277
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-cancel-06.kt).
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-07.kt).
314
+
<!--- KNIT example-cancel-07.kt -->
315
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-cancel-07.kt).
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-08.kt).
357
+
<!--- KNIT example-cancel-08.kt -->
358
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-cancel-08.kt).
358
359
>
359
-
{type="note"}
360
+
{style="note"}
360
361
361
362
There is no longer an exception when running this code:
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-09.kt).
418
+
<!--- KNIT example-cancel-09.kt -->
419
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-cancel-09.kt).
419
420
>
420
-
{type="note"}
421
+
{style="note"}
421
422
422
423
<!--- CLEAR -->
423
424
@@ -428,7 +429,7 @@ of your machine. You may need to tweak the timeout in this example to actually s
428
429
> since it always happens from the same thread, the one used by `runBlocking`.
429
430
> More on that will be explained in the chapter on coroutine context.
430
431
>
431
-
{type="note"}
432
+
{style="note"}
432
433
433
434
To work around this problem you can store a reference to the resource in a variable instead of returning it
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-cancel-10.kt).
471
+
<!--- KNIT example-cancel-10.kt -->
472
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-cancel-10.kt).
472
473
>
473
-
{type="note"}
474
+
{style="note"}
474
475
475
476
This example always prints zero. Resources do not leak.
0 commit comments