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).
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).
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).
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).
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).
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).
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).
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).
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).
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).
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.
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-channel-01.kt).
35
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-channel-01.kt).
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-channel-02.kt).
81
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-channel-02.kt).
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-channel-03.kt).
122
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-channel-03.kt).
122
123
>
123
-
{type="note"}
124
+
{style="note"}
124
125
125
126
<!--- TEST
126
127
1
@@ -182,9 +183,9 @@ fun CoroutineScope.square(numbers: ReceiveChannel<Int>): ReceiveChannel<Int> = p
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-channel-04.kt).
186
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-channel-04.kt).
186
187
>
187
-
{type="note"}
188
+
{style="note"}
188
189
189
190
<!--- TEST
190
191
1
@@ -199,7 +200,7 @@ Done!
199
200
> so that we can rely on [structured concurrency](composing-suspending-functions.md#structured-concurrency-with-async) to make
200
201
> sure that we don't have lingering global coroutines in our application.
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-channel-05.kt).
270
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-channel-05.kt).
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-channel-06.kt).
364
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-channel-06.kt).
364
365
>
365
-
{type="note"}
366
+
{style="note"}
366
367
367
368
The output will be similar to the following one, albeit the processor ids that receive
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-channel-07.kt).
439
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-channel-07.kt).
439
440
>
440
-
{type="note"}
441
+
{style="note"}
441
442
442
443
The output is:
443
444
@@ -485,9 +486,9 @@ fun main() = runBlocking<Unit> {
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-channel-08.kt).
489
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-channel-08.kt).
489
490
>
490
-
{type="note"}
491
+
{style="note"}
491
492
492
493
It prints "sending" _five_ times using a buffered channel with capacity of _four_:
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-channel-09.kt).
542
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-channel-09.kt).
542
543
>
543
-
{type="note"}
544
+
{style="note"}
544
545
545
546
The "ping" coroutine is started first, so it is the first one to receive the ball. Even though "ping"
546
547
coroutine immediately starts receiving the ball again after sending it back to the table, the ball gets
@@ -602,9 +603,9 @@ fun main() = runBlocking<Unit> {
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-channel-10.kt).
606
+
> You can get the full code [here](https://github.com/Kotlin/kotlinx.coroutines/blob/master/kotlinx-coroutines-core/jvm/test/guide/example-channel-10.kt).
0 commit comments