Roman Elizarov | 43e9011 | 2017-05-10 11:25:20 +0300 | [diff] [blame] | 1 | <!--- INCLUDE .*/example-([a-z]+)-([0-9a-z]+)\.kt |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2016-2017 JetBrains s.r.o. |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 17 | |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 18 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. |
| 19 | package guide.$$1.example$$2 |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 20 | |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 21 | import kotlinx.coroutines.experimental.* |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 22 | --> |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 23 | <!--- KNIT core/kotlinx-coroutines-core/src/test/kotlin/guide/.*\.kt --> |
| 24 | <!--- TEST_OUT core/kotlinx-coroutines-core/src/test/kotlin/guide/test/GuideTest.kt |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 25 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. |
| 26 | package guide.test |
| 27 | |
| 28 | import org.junit.Test |
| 29 | |
| 30 | class GuideTest { |
| 31 | --> |
Roman Elizarov | f16fd27 | 2017-02-07 11:26:00 +0300 | [diff] [blame] | 32 | |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 33 | # Guide to kotlinx.coroutines by example |
| 34 | |
Roman Elizarov | a4d45d2 | 2017-11-20 16:47:09 +0300 | [diff] [blame] | 35 | This is a guide on core features of `kotlinx.coroutines` with a series of examples. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 36 | |
Roman Elizarov | 2a63892 | 2017-03-04 10:22:43 +0300 | [diff] [blame] | 37 | ## Introduction and setup |
| 38 | |
| 39 | Kotlin, as a language, provides only minimal low-level APIs in its standard library to enable various other |
| 40 | libraries to utilize coroutines. Unlike many other languages with similar capabilities, `async` and `await` |
| 41 | are not keywords in Kotlin and are not even part of its standard library. |
| 42 | |
Robert Hencke | 497d343 | 2017-04-11 00:14:29 -0400 | [diff] [blame] | 43 | `kotlinx.coroutines` is one such rich library. It contains a number of high-level |
Roman Elizarov | a4d45d2 | 2017-11-20 16:47:09 +0300 | [diff] [blame] | 44 | coroutine-enabled primitives that this guide covers, including `launch`, `async` and others. |
Roman Elizarov | 2a63892 | 2017-03-04 10:22:43 +0300 | [diff] [blame] | 45 | You need to add a dependency on `kotlinx-coroutines-core` module as explained |
| 46 | [here](README.md#using-in-your-projects) to use primitives from this guide in your projects. |
| 47 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 48 | ## Table of contents |
| 49 | |
Roman Elizarov | fa7723e | 2017-02-06 11:17:51 +0300 | [diff] [blame] | 50 | <!--- TOC --> |
| 51 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 52 | * [Coroutine basics](#coroutine-basics) |
| 53 | * [Your first coroutine](#your-first-coroutine) |
| 54 | * [Bridging blocking and non-blocking worlds](#bridging-blocking-and-non-blocking-worlds) |
| 55 | * [Waiting for a job](#waiting-for-a-job) |
| 56 | * [Extract function refactoring](#extract-function-refactoring) |
| 57 | * [Coroutines ARE light-weight](#coroutines-are-light-weight) |
| 58 | * [Coroutines are like daemon threads](#coroutines-are-like-daemon-threads) |
| 59 | * [Cancellation and timeouts](#cancellation-and-timeouts) |
| 60 | * [Cancelling coroutine execution](#cancelling-coroutine-execution) |
| 61 | * [Cancellation is cooperative](#cancellation-is-cooperative) |
| 62 | * [Making computation code cancellable](#making-computation-code-cancellable) |
| 63 | * [Closing resources with finally](#closing-resources-with-finally) |
| 64 | * [Run non-cancellable block](#run-non-cancellable-block) |
| 65 | * [Timeout](#timeout) |
| 66 | * [Composing suspending functions](#composing-suspending-functions) |
| 67 | * [Sequential by default](#sequential-by-default) |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 68 | * [Concurrent using async](#concurrent-using-async) |
| 69 | * [Lazily started async](#lazily-started-async) |
| 70 | * [Async-style functions](#async-style-functions) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 71 | * [Coroutine context and dispatchers](#coroutine-context-and-dispatchers) |
Roman Elizarov | fa7723e | 2017-02-06 11:17:51 +0300 | [diff] [blame] | 72 | * [Dispatchers and threads](#dispatchers-and-threads) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 73 | * [Unconfined vs confined dispatcher](#unconfined-vs-confined-dispatcher) |
| 74 | * [Debugging coroutines and threads](#debugging-coroutines-and-threads) |
| 75 | * [Jumping between threads](#jumping-between-threads) |
| 76 | * [Job in the context](#job-in-the-context) |
| 77 | * [Children of a coroutine](#children-of-a-coroutine) |
| 78 | * [Combining contexts](#combining-contexts) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 79 | * [Parental responsibilities](#parental-responsibilities) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 80 | * [Naming coroutines for debugging](#naming-coroutines-for-debugging) |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 81 | * [Cancellation via explicit job](#cancellation-via-explicit-job) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 82 | * [Channels](#channels) |
| 83 | * [Channel basics](#channel-basics) |
| 84 | * [Closing and iteration over channels](#closing-and-iteration-over-channels) |
| 85 | * [Building channel producers](#building-channel-producers) |
| 86 | * [Pipelines](#pipelines) |
| 87 | * [Prime numbers with pipeline](#prime-numbers-with-pipeline) |
| 88 | * [Fan-out](#fan-out) |
| 89 | * [Fan-in](#fan-in) |
| 90 | * [Buffered channels](#buffered-channels) |
Roman Elizarov | b0517ba | 2017-02-27 14:03:14 +0300 | [diff] [blame] | 91 | * [Channels are fair](#channels-are-fair) |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 92 | * [Shared mutable state and concurrency](#shared-mutable-state-and-concurrency) |
| 93 | * [The problem](#the-problem) |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 94 | * [Volatiles are of no help](#volatiles-are-of-no-help) |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 95 | * [Thread-safe data structures](#thread-safe-data-structures) |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 96 | * [Thread confinement fine-grained](#thread-confinement-fine-grained) |
| 97 | * [Thread confinement coarse-grained](#thread-confinement-coarse-grained) |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 98 | * [Mutual exclusion](#mutual-exclusion) |
| 99 | * [Actors](#actors) |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 100 | * [Select expression](#select-expression) |
| 101 | * [Selecting from channels](#selecting-from-channels) |
| 102 | * [Selecting on close](#selecting-on-close) |
| 103 | * [Selecting to send](#selecting-to-send) |
| 104 | * [Selecting deferred values](#selecting-deferred-values) |
| 105 | * [Switch over a channel of deferred values](#switch-over-a-channel-of-deferred-values) |
Roman Elizarov | 8db1733 | 2017-03-09 12:40:45 +0300 | [diff] [blame] | 106 | * [Further reading](#further-reading) |
Roman Elizarov | fa7723e | 2017-02-06 11:17:51 +0300 | [diff] [blame] | 107 | |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 108 | <!--- END_TOC --> |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 109 | |
| 110 | ## Coroutine basics |
| 111 | |
| 112 | This section covers basic coroutine concepts. |
| 113 | |
| 114 | ### Your first coroutine |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 115 | |
| 116 | Run the following code: |
| 117 | |
| 118 | ```kotlin |
| 119 | fun main(args: Array<String>) { |
Roman Elizarov | a4d45d2 | 2017-11-20 16:47:09 +0300 | [diff] [blame] | 120 | launch { // launch new coroutine in background and continue |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 121 | delay(1000L) // non-blocking delay for 1 second (default time unit is ms) |
| 122 | println("World!") // print after delay |
| 123 | } |
Roman Elizarov | a4d45d2 | 2017-11-20 16:47:09 +0300 | [diff] [blame] | 124 | println("Hello,") // main thread continues while coroutine is delayed |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 125 | Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive |
| 126 | } |
| 127 | ``` |
| 128 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 129 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-01.kt) |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 130 | |
| 131 | Run this code: |
| 132 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 133 | ```text |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 134 | Hello, |
| 135 | World! |
| 136 | ``` |
| 137 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 138 | <!--- TEST --> |
| 139 | |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 140 | Essentially, coroutines are light-weight threads. |
| 141 | They are launched with [launch] _coroutine builder_. |
| 142 | You can achieve the same result replacing |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 143 | `launch { ... }` with `thread { ... }` and `delay(...)` with `Thread.sleep(...)`. Try it. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 144 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 145 | If you start by replacing `launch` by `thread`, the compiler produces the following error: |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 146 | |
| 147 | ``` |
| 148 | Error: Kotlin: Suspend functions are only allowed to be called from a coroutine or another suspend function |
| 149 | ``` |
| 150 | |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 151 | That is because [delay] is a special _suspending function_ that does not block a thread, but _suspends_ |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 152 | coroutine and it can be only used from a coroutine. |
| 153 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 154 | ### Bridging blocking and non-blocking worlds |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 155 | |
Roman Elizarov | a4d45d2 | 2017-11-20 16:47:09 +0300 | [diff] [blame] | 156 | The first example mixes _non-blocking_ `delay(...)` and _blocking_ `Thread.sleep(...)` in the same code. |
| 157 | It is easy to get lost which one is blocking and which one is not. |
| 158 | Let's be explicit about blocking using [runBlocking] coroutine builder: |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 159 | |
| 160 | ```kotlin |
Roman Elizarov | a4d45d2 | 2017-11-20 16:47:09 +0300 | [diff] [blame] | 161 | fun main(args: Array<String>) { |
| 162 | launch { // launch new coroutine in background and continue |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 163 | delay(1000L) |
| 164 | println("World!") |
| 165 | } |
Roman Elizarov | a4d45d2 | 2017-11-20 16:47:09 +0300 | [diff] [blame] | 166 | println("Hello,") // main thread continues here immediately |
| 167 | runBlocking { // but this expression blocks the main thread |
| 168 | delay(2000L) // ... while we delay for 2 seconds to keep JVM alive |
| 169 | } |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 170 | } |
| 171 | ``` |
| 172 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 173 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-02.kt) |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 174 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 175 | <!--- TEST |
| 176 | Hello, |
| 177 | World! |
| 178 | --> |
| 179 | |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 180 | The result is the same, but this code uses only non-blocking [delay]. |
Tylos | 81451de | 2017-12-17 21:33:17 +0100 | [diff] [blame] | 181 | The main thread, that invokes `runBlocking`, _blocks_ until the coroutine inside `runBlocking` completes. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 182 | |
Roman Elizarov | a4d45d2 | 2017-11-20 16:47:09 +0300 | [diff] [blame] | 183 | This example can be also rewritten in a more idiomatic way, using `runBlocking` to wrap |
| 184 | the execution of the main function: |
| 185 | |
| 186 | ```kotlin |
| 187 | fun main(args: Array<String>) = runBlocking<Unit> { // start main coroutine |
| 188 | launch { // launch new coroutine in background and continue |
| 189 | delay(1000L) |
| 190 | println("World!") |
| 191 | } |
| 192 | println("Hello,") // main coroutine continues here immediately |
| 193 | delay(2000L) // delaying for 2 seconds to keep JVM alive |
| 194 | } |
| 195 | ``` |
| 196 | |
| 197 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-02b.kt) |
| 198 | |
| 199 | <!--- TEST |
| 200 | Hello, |
| 201 | World! |
| 202 | --> |
| 203 | |
| 204 | Here `runBlocking<Unit> { ... }` works as an adaptor that is used to start the top-level main coroutine. |
| 205 | We explicitly specify its `Unit` return type, because a well-formed `main` function in Kotlin has to return `Unit`. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 206 | |
| 207 | This is also a way to write unit-tests for suspending functions: |
| 208 | |
| 209 | ```kotlin |
| 210 | class MyTest { |
| 211 | @Test |
| 212 | fun testMySuspendingFunction() = runBlocking<Unit> { |
| 213 | // here we can use suspending functions using any assertion style that we like |
| 214 | } |
| 215 | } |
| 216 | ``` |
Roman Elizarov | b3d55a5 | 2017-02-03 12:47:21 +0300 | [diff] [blame] | 217 | |
| 218 | <!--- CLEAR --> |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 219 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 220 | ### Waiting for a job |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 221 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 222 | Delaying for a time while another coroutine is working is not a good approach. Let's explicitly |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 223 | wait (in a non-blocking way) until the background [Job] that we have launched is complete: |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 224 | |
| 225 | ```kotlin |
| 226 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 227 | val job = launch { // launch new coroutine and keep a reference to its Job |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 228 | delay(1000L) |
| 229 | println("World!") |
| 230 | } |
| 231 | println("Hello,") |
| 232 | job.join() // wait until child coroutine completes |
| 233 | } |
| 234 | ``` |
| 235 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 236 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-03.kt) |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 237 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 238 | <!--- TEST |
| 239 | Hello, |
| 240 | World! |
| 241 | --> |
| 242 | |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 243 | Now the result is still the same, but the code of the main coroutine is not tied to the duration of |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 244 | the background job in any way. Much better. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 245 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 246 | ### Extract function refactoring |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 247 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 248 | Let's extract the block of code inside `launch { ... }` into a separate function. When you |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 249 | perform "Extract function" refactoring on this code you get a new function with `suspend` modifier. |
| 250 | That is your first _suspending function_. Suspending functions can be used inside coroutines |
| 251 | just like regular functions, but their additional feature is that they can, in turn, |
| 252 | use other suspending functions, like `delay` in this example, to _suspend_ execution of a coroutine. |
| 253 | |
| 254 | ```kotlin |
| 255 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 256 | val job = launch { doWorld() } |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 257 | println("Hello,") |
| 258 | job.join() |
| 259 | } |
| 260 | |
| 261 | // this is your first suspending function |
| 262 | suspend fun doWorld() { |
| 263 | delay(1000L) |
| 264 | println("World!") |
| 265 | } |
| 266 | ``` |
| 267 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 268 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-04.kt) |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 269 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 270 | <!--- TEST |
| 271 | Hello, |
| 272 | World! |
| 273 | --> |
| 274 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 275 | ### Coroutines ARE light-weight |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 276 | |
| 277 | Run the following code: |
| 278 | |
| 279 | ```kotlin |
| 280 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 281 | val jobs = List(100_000) { // launch a lot of coroutines and list their jobs |
| 282 | launch { |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 283 | delay(1000L) |
| 284 | print(".") |
| 285 | } |
| 286 | } |
| 287 | jobs.forEach { it.join() } // wait for all jobs to complete |
| 288 | } |
| 289 | ``` |
| 290 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 291 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-05.kt) |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 292 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 293 | <!--- TEST lines.size == 1 && lines[0] == ".".repeat(100_000) --> |
| 294 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 295 | It launches 100K coroutines and, after a second, each coroutine prints a dot. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 296 | Now, try that with threads. What would happen? (Most likely your code will produce some sort of out-of-memory error) |
| 297 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 298 | ### Coroutines are like daemon threads |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 299 | |
| 300 | The following code launches a long-running coroutine that prints "I'm sleeping" twice a second and then |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 301 | returns from the main function after some delay: |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 302 | |
| 303 | ```kotlin |
| 304 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 305 | launch { |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 306 | repeat(1000) { i -> |
| 307 | println("I'm sleeping $i ...") |
| 308 | delay(500L) |
| 309 | } |
| 310 | } |
| 311 | delay(1300L) // just quit after delay |
| 312 | } |
| 313 | ``` |
| 314 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 315 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-basic-06.kt) |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 316 | |
| 317 | You can run and see that it prints three lines and terminates: |
| 318 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 319 | ```text |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 320 | I'm sleeping 0 ... |
| 321 | I'm sleeping 1 ... |
| 322 | I'm sleeping 2 ... |
| 323 | ``` |
| 324 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 325 | <!--- TEST --> |
| 326 | |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 327 | Active coroutines do not keep the process alive. They are like daemon threads. |
| 328 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 329 | ## Cancellation and timeouts |
| 330 | |
| 331 | This section covers coroutine cancellation and timeouts. |
| 332 | |
| 333 | ### Cancelling coroutine execution |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 334 | |
| 335 | In small application the return from "main" method might sound like a good idea to get all coroutines |
| 336 | implicitly terminated. In a larger, long-running application, you need finer-grained control. |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 337 | The [launch] function returns a [Job] that can be used to cancel running coroutine: |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 338 | |
| 339 | ```kotlin |
| 340 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 341 | val job = launch { |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 342 | repeat(1000) { i -> |
| 343 | println("I'm sleeping $i ...") |
| 344 | delay(500L) |
| 345 | } |
| 346 | } |
| 347 | delay(1300L) // delay a bit |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 348 | println("main: I'm tired of waiting!") |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 349 | job.cancel() // cancels the job |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 350 | job.join() // waits for job's completion |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 351 | println("main: Now I can quit.") |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 352 | } |
| 353 | ``` |
| 354 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 355 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-01.kt) |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 356 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 357 | It produces the following output: |
| 358 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 359 | ```text |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 360 | I'm sleeping 0 ... |
| 361 | I'm sleeping 1 ... |
| 362 | I'm sleeping 2 ... |
| 363 | main: I'm tired of waiting! |
| 364 | main: Now I can quit. |
| 365 | ``` |
| 366 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 367 | <!--- TEST --> |
| 368 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 369 | As soon as main invokes `job.cancel`, we don't see any output from the other coroutine because it was cancelled. |
Roman Elizarov | 8839673 | 2017-09-27 21:30:47 +0300 | [diff] [blame] | 370 | There is also a [Job] extension function [cancelAndJoin] |
| 371 | that combines [cancel][Job.cancel] and [join][Job.join] invocations. |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 372 | |
| 373 | ### Cancellation is cooperative |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 374 | |
Tair Rzayev | af73462 | 2017-02-01 22:30:16 +0200 | [diff] [blame] | 375 | Coroutine cancellation is _cooperative_. A coroutine code has to cooperate to be cancellable. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 376 | All the suspending functions in `kotlinx.coroutines` are _cancellable_. They check for cancellation of |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 377 | coroutine and throw [CancellationException] when cancelled. However, if a coroutine is working in |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 378 | a computation and does not check for cancellation, then it cannot be cancelled, like the following |
| 379 | example shows: |
| 380 | |
| 381 | ```kotlin |
| 382 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 24cd654 | 2017-08-03 21:20:04 -0700 | [diff] [blame] | 383 | val startTime = System.currentTimeMillis() |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 384 | val job = launch { |
Roman Elizarov | 24cd654 | 2017-08-03 21:20:04 -0700 | [diff] [blame] | 385 | var nextPrintTime = startTime |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 386 | var i = 0 |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 387 | while (i < 5) { // computation loop, just wastes CPU |
Roman Elizarov | 24cd654 | 2017-08-03 21:20:04 -0700 | [diff] [blame] | 388 | // print a message twice a second |
| 389 | if (System.currentTimeMillis() >= nextPrintTime) { |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 390 | println("I'm sleeping ${i++} ...") |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 391 | nextPrintTime += 500L |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 392 | } |
| 393 | } |
| 394 | } |
| 395 | delay(1300L) // delay a bit |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 396 | println("main: I'm tired of waiting!") |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 397 | job.cancelAndJoin() // cancels the job and waits for its completion |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 398 | println("main: Now I can quit.") |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 399 | } |
| 400 | ``` |
| 401 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 402 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-02.kt) |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 403 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 404 | Run it to see that it continues to print "I'm sleeping" even after cancellation |
| 405 | until the job completes by itself after five iterations. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 406 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 407 | <!--- TEST |
| 408 | I'm sleeping 0 ... |
| 409 | I'm sleeping 1 ... |
| 410 | I'm sleeping 2 ... |
| 411 | main: I'm tired of waiting! |
| 412 | I'm sleeping 3 ... |
| 413 | I'm sleeping 4 ... |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 414 | main: Now I can quit. |
| 415 | --> |
| 416 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 417 | ### Making computation code cancellable |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 418 | |
| 419 | There are two approaches to making computation code cancellable. The first one is to periodically |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 420 | invoke a suspending function that checks for cancellation. There is a [yield] function that is a good choice for that purpose. |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 421 | The other one is to explicitly check the cancellation status. Let us try the later approach. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 422 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 423 | Replace `while (i < 5)` in the previous example with `while (isActive)` and rerun it. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 424 | |
Roman Elizarov | b3d55a5 | 2017-02-03 12:47:21 +0300 | [diff] [blame] | 425 | ```kotlin |
| 426 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 24cd654 | 2017-08-03 21:20:04 -0700 | [diff] [blame] | 427 | val startTime = System.currentTimeMillis() |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 428 | val job = launch { |
Roman Elizarov | 24cd654 | 2017-08-03 21:20:04 -0700 | [diff] [blame] | 429 | var nextPrintTime = startTime |
Roman Elizarov | b3d55a5 | 2017-02-03 12:47:21 +0300 | [diff] [blame] | 430 | var i = 0 |
| 431 | while (isActive) { // cancellable computation loop |
Roman Elizarov | 24cd654 | 2017-08-03 21:20:04 -0700 | [diff] [blame] | 432 | // print a message twice a second |
| 433 | if (System.currentTimeMillis() >= nextPrintTime) { |
Roman Elizarov | b3d55a5 | 2017-02-03 12:47:21 +0300 | [diff] [blame] | 434 | println("I'm sleeping ${i++} ...") |
Roman Elizarov | 24cd654 | 2017-08-03 21:20:04 -0700 | [diff] [blame] | 435 | nextPrintTime += 500L |
Roman Elizarov | b3d55a5 | 2017-02-03 12:47:21 +0300 | [diff] [blame] | 436 | } |
| 437 | } |
| 438 | } |
| 439 | delay(1300L) // delay a bit |
| 440 | println("main: I'm tired of waiting!") |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 441 | job.cancelAndJoin() // cancels the job and waits for its completion |
Roman Elizarov | b3d55a5 | 2017-02-03 12:47:21 +0300 | [diff] [blame] | 442 | println("main: Now I can quit.") |
| 443 | } |
| 444 | ``` |
| 445 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 446 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-03.kt) |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 447 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 448 | As you can see, now this loop is cancelled. [isActive][CoroutineScope.isActive] is a property that is available inside |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 449 | the code of coroutines via [CoroutineScope] object. |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 450 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 451 | <!--- TEST |
| 452 | I'm sleeping 0 ... |
| 453 | I'm sleeping 1 ... |
| 454 | I'm sleeping 2 ... |
| 455 | main: I'm tired of waiting! |
| 456 | main: Now I can quit. |
| 457 | --> |
| 458 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 459 | ### Closing resources with finally |
| 460 | |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 461 | Cancellable suspending functions throw [CancellationException] on cancellation which can be handled in |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 462 | all the usual way. For example, `try {...} finally {...}` expression and Kotlin `use` function execute their |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 463 | finalization actions normally when coroutine is cancelled: |
| 464 | |
| 465 | ```kotlin |
| 466 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 467 | val job = launch { |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 468 | try { |
| 469 | repeat(1000) { i -> |
| 470 | println("I'm sleeping $i ...") |
| 471 | delay(500L) |
| 472 | } |
| 473 | } finally { |
| 474 | println("I'm running finally") |
| 475 | } |
| 476 | } |
| 477 | delay(1300L) // delay a bit |
| 478 | println("main: I'm tired of waiting!") |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 479 | job.cancelAndJoin() // cancels the job and waits for its completion |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 480 | println("main: Now I can quit.") |
| 481 | } |
| 482 | ``` |
| 483 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 484 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-04.kt) |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 485 | |
Roman Elizarov | 8839673 | 2017-09-27 21:30:47 +0300 | [diff] [blame] | 486 | Both [join][Job.join] and [cancelAndJoin] wait for all the finalization actions to complete, |
| 487 | so the example above produces the following output: |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 488 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 489 | ```text |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 490 | I'm sleeping 0 ... |
| 491 | I'm sleeping 1 ... |
| 492 | I'm sleeping 2 ... |
| 493 | main: I'm tired of waiting! |
| 494 | I'm running finally |
| 495 | main: Now I can quit. |
| 496 | ``` |
| 497 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 498 | <!--- TEST --> |
| 499 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 500 | ### Run non-cancellable block |
| 501 | |
| 502 | Any attempt to use a suspending function in the `finally` block of the previous example will cause |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 503 | [CancellationException], because the coroutine running this code is cancelled. Usually, this is not a |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 504 | problem, since all well-behaving closing operations (closing a file, cancelling a job, or closing any kind of a |
| 505 | communication channel) are usually non-blocking and do not involve any suspending functions. However, in the |
| 506 | rare case when you need to suspend in the cancelled coroutine you can wrap the corresponding code in |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 507 | `withContext(NonCancellable) {...}` using [withContext] function and [NonCancellable] context as the following example shows: |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 508 | |
| 509 | ```kotlin |
| 510 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 511 | val job = launch { |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 512 | try { |
| 513 | repeat(1000) { i -> |
| 514 | println("I'm sleeping $i ...") |
| 515 | delay(500L) |
| 516 | } |
| 517 | } finally { |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 518 | withContext(NonCancellable) { |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 519 | println("I'm running finally") |
| 520 | delay(1000L) |
| 521 | println("And I've just delayed for 1 sec because I'm non-cancellable") |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | delay(1300L) // delay a bit |
| 526 | println("main: I'm tired of waiting!") |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 527 | job.cancelAndJoin() // cancels the job and waits for its completion |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 528 | println("main: Now I can quit.") |
| 529 | } |
| 530 | ``` |
| 531 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 532 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-05.kt) |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 533 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 534 | <!--- TEST |
| 535 | I'm sleeping 0 ... |
| 536 | I'm sleeping 1 ... |
| 537 | I'm sleeping 2 ... |
| 538 | main: I'm tired of waiting! |
| 539 | I'm running finally |
| 540 | And I've just delayed for 1 sec because I'm non-cancellable |
| 541 | main: Now I can quit. |
| 542 | --> |
| 543 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 544 | ### Timeout |
| 545 | |
| 546 | The most obvious reason to cancel coroutine execution in practice, |
| 547 | is because its execution time has exceeded some timeout. |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 548 | While you can manually track the reference to the corresponding [Job] and launch a separate coroutine to cancel |
| 549 | the tracked one after delay, there is a ready to use [withTimeout] function that does it. |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 550 | Look at the following example: |
| 551 | |
| 552 | ```kotlin |
| 553 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 554 | withTimeout(1300L) { |
| 555 | repeat(1000) { i -> |
| 556 | println("I'm sleeping $i ...") |
| 557 | delay(500L) |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | ``` |
| 562 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 563 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-06.kt) |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 564 | |
| 565 | It produces the following output: |
| 566 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 567 | ```text |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 568 | I'm sleeping 0 ... |
| 569 | I'm sleeping 1 ... |
| 570 | I'm sleeping 2 ... |
Roman Elizarov | 63f6ea2 | 2017-09-06 18:42:34 +0300 | [diff] [blame] | 571 | Exception in thread "main" kotlinx.coroutines.experimental.TimeoutCancellationException: Timed out waiting for 1300 MILLISECONDS |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 572 | ``` |
| 573 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 574 | <!--- TEST STARTS_WITH --> |
| 575 | |
Roman Elizarov | 63f6ea2 | 2017-09-06 18:42:34 +0300 | [diff] [blame] | 576 | The `TimeoutCancellationException` that is thrown by [withTimeout] is a subclass of [CancellationException]. |
Roman Elizarov | ca9d5be | 2017-04-20 19:23:18 +0300 | [diff] [blame] | 577 | We have not seen its stack trace printed on the console before. That is because |
Roman Elizarov | 7c864d8 | 2017-02-27 10:17:50 +0300 | [diff] [blame] | 578 | inside a cancelled coroutine `CancellationException` is considered to be a normal reason for coroutine completion. |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 579 | However, in this example we have used `withTimeout` right inside the `main` function. |
| 580 | |
| 581 | Because cancellation is just an exception, all the resources will be closed in a usual way. |
Roman Elizarov | 63f6ea2 | 2017-09-06 18:42:34 +0300 | [diff] [blame] | 582 | You can wrap the code with timeout in `try {...} catch (e: TimeoutCancellationException) {...}` block if |
| 583 | you need to do some additional action specifically on any kind of timeout or use [withTimeoutOrNull] function |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 584 | that is similar to [withTimeout], but returns `null` on timeout instead of throwing an exception: |
| 585 | |
| 586 | ```kotlin |
| 587 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 588 | val result = withTimeoutOrNull(1300L) { |
| 589 | repeat(1000) { i -> |
| 590 | println("I'm sleeping $i ...") |
| 591 | delay(500L) |
| 592 | } |
| 593 | "Done" // will get cancelled before it produces this result |
| 594 | } |
| 595 | println("Result is $result") |
| 596 | } |
| 597 | ``` |
| 598 | |
| 599 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-cancel-07.kt) |
| 600 | |
| 601 | There is no longer an exception when running this code: |
| 602 | |
| 603 | ```text |
| 604 | I'm sleeping 0 ... |
| 605 | I'm sleeping 1 ... |
| 606 | I'm sleeping 2 ... |
| 607 | Result is null |
| 608 | ``` |
| 609 | |
| 610 | <!--- TEST --> |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 611 | |
| 612 | ## Composing suspending functions |
| 613 | |
| 614 | This section covers various approaches to composition of suspending functions. |
| 615 | |
| 616 | ### Sequential by default |
| 617 | |
| 618 | Assume that we have two suspending functions defined elsewhere that do something useful like some kind of |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 619 | remote service call or computation. We just pretend they are useful, but actually each one just |
| 620 | delays for a second for the purpose of this example: |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 621 | |
Roman Elizarov | fa7723e | 2017-02-06 11:17:51 +0300 | [diff] [blame] | 622 | <!--- INCLUDE .*/example-compose-([0-9]+).kt |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 623 | import kotlin.system.* |
Roman Elizarov | fa7723e | 2017-02-06 11:17:51 +0300 | [diff] [blame] | 624 | --> |
| 625 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 626 | ```kotlin |
| 627 | suspend fun doSomethingUsefulOne(): Int { |
| 628 | delay(1000L) // pretend we are doing something useful here |
| 629 | return 13 |
| 630 | } |
| 631 | |
| 632 | suspend fun doSomethingUsefulTwo(): Int { |
| 633 | delay(1000L) // pretend we are doing something useful here, too |
| 634 | return 29 |
| 635 | } |
| 636 | ``` |
| 637 | |
Roman Elizarov | fa7723e | 2017-02-06 11:17:51 +0300 | [diff] [blame] | 638 | <!--- INCLUDE .*/example-compose-([0-9]+).kt --> |
| 639 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 640 | What do we do if need to invoke them _sequentially_ -- first `doSomethingUsefulOne` _and then_ |
| 641 | `doSomethingUsefulTwo` and compute the sum of their results? |
Ronen Sabag | d2d42ea | 2017-12-24 21:55:06 +0200 | [diff] [blame] | 642 | In practice we do this if we use the results of the first function to make a decision on whether we need |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 643 | to invoke the second one or to decide on how to invoke it. |
| 644 | |
| 645 | We just use a normal sequential invocation, because the code in the coroutine, just like in the regular |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 646 | code, is _sequential_ by default. The following example demonstrates it by measuring the total |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 647 | time it takes to execute both suspending functions: |
| 648 | |
| 649 | ```kotlin |
| 650 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 651 | val time = measureTimeMillis { |
| 652 | val one = doSomethingUsefulOne() |
| 653 | val two = doSomethingUsefulTwo() |
| 654 | println("The answer is ${one + two}") |
| 655 | } |
| 656 | println("Completed in $time ms") |
| 657 | } |
| 658 | ``` |
| 659 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 660 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-01.kt) |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 661 | |
| 662 | It produces something like this: |
| 663 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 664 | ```text |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 665 | The answer is 42 |
| 666 | Completed in 2017 ms |
| 667 | ``` |
| 668 | |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 669 | <!--- TEST ARBITRARY_TIME --> |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 670 | |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 671 | ### Concurrent using async |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 672 | |
| 673 | What if there are no dependencies between invocation of `doSomethingUsefulOne` and `doSomethingUsefulTwo` and |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 674 | we want to get the answer faster, by doing both _concurrently_? This is where [async] comes to help. |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 675 | |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 676 | Conceptually, [async] is just like [launch]. It starts a separate coroutine which is a light-weight thread |
| 677 | that works concurrently with all the other coroutines. The difference is that `launch` returns a [Job] and |
| 678 | does not carry any resulting value, while `async` returns a [Deferred] -- a light-weight non-blocking future |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 679 | that represents a promise to provide a result later. You can use `.await()` on a deferred value to get its eventual result, |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 680 | but `Deferred` is also a `Job`, so you can cancel it if needed. |
| 681 | |
| 682 | ```kotlin |
| 683 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 684 | val time = measureTimeMillis { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 685 | val one = async { doSomethingUsefulOne() } |
| 686 | val two = async { doSomethingUsefulTwo() } |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 687 | println("The answer is ${one.await() + two.await()}") |
| 688 | } |
| 689 | println("Completed in $time ms") |
| 690 | } |
| 691 | ``` |
| 692 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 693 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-02.kt) |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 694 | |
| 695 | It produces something like this: |
| 696 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 697 | ```text |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 698 | The answer is 42 |
| 699 | Completed in 1017 ms |
| 700 | ``` |
| 701 | |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 702 | <!--- TEST ARBITRARY_TIME --> |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 703 | |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 704 | This is twice as fast, because we have concurrent execution of two coroutines. |
| 705 | Note, that concurrency with coroutines is always explicit. |
| 706 | |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 707 | ### Lazily started async |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 708 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 709 | There is a laziness option to [async] using an optional `start` parameter with a value of [CoroutineStart.LAZY]. |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 710 | It starts coroutine only when its result is needed by some |
| 711 | [await][Deferred.await] or if a [start][Job.start] function |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 712 | is invoked. Run the following example that differs from the previous one only by this option: |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 713 | |
| 714 | ```kotlin |
| 715 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 716 | val time = measureTimeMillis { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 717 | val one = async(start = CoroutineStart.LAZY) { doSomethingUsefulOne() } |
| 718 | val two = async(start = CoroutineStart.LAZY) { doSomethingUsefulTwo() } |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 719 | println("The answer is ${one.await() + two.await()}") |
| 720 | } |
| 721 | println("Completed in $time ms") |
| 722 | } |
| 723 | ``` |
| 724 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 725 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-03.kt) |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 726 | |
| 727 | It produces something like this: |
| 728 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 729 | ```text |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 730 | The answer is 42 |
| 731 | Completed in 2017 ms |
| 732 | ``` |
| 733 | |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 734 | <!--- TEST ARBITRARY_TIME --> |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 735 | |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 736 | So, we are back to sequential execution, because we _first_ start and await for `one`, _and then_ start and await |
| 737 | for `two`. It is not the intended use-case for laziness. It is designed as a replacement for |
| 738 | the standard `lazy` function in cases when computation of the value involves suspending functions. |
| 739 | |
| 740 | ### Async-style functions |
| 741 | |
| 742 | We can define async-style functions that invoke `doSomethingUsefulOne` and `doSomethingUsefulTwo` |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 743 | _asynchronously_ using [async] coroutine builder. It is a good style to name such functions with |
Marcin Moskała | 7e94e70 | 2018-01-29 18:39:02 +0100 | [diff] [blame] | 744 | "Async" suffix to highlight the fact that they only start asynchronous computation and one needs |
| 745 | to use the resulting deferred value to get the result. |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 746 | |
| 747 | ```kotlin |
Marcin Moskała | 7e94e70 | 2018-01-29 18:39:02 +0100 | [diff] [blame] | 748 | // The result type of somethingUsefulOneAsync is Deferred<Int> |
| 749 | fun somethingUsefulOneAsync() = async { |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 750 | doSomethingUsefulOne() |
| 751 | } |
| 752 | |
Marcin Moskała | 7e94e70 | 2018-01-29 18:39:02 +0100 | [diff] [blame] | 753 | // The result type of somethingUsefulTwoAsync is Deferred<Int> |
| 754 | fun somethingUsefulTwoAsync() = async { |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 755 | doSomethingUsefulTwo() |
| 756 | } |
| 757 | ``` |
| 758 | |
Marcin Moskała | 7e94e70 | 2018-01-29 18:39:02 +0100 | [diff] [blame] | 759 | Note, that these `xxxAsync` functions are **not** _suspending_ functions. They can be used from anywhere. |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 760 | However, their use always implies asynchronous (here meaning _concurrent_) execution of their action |
| 761 | with the invoking code. |
| 762 | |
| 763 | The following example shows their use outside of coroutine: |
| 764 | |
| 765 | ```kotlin |
| 766 | // note, that we don't have `runBlocking` to the right of `main` in this example |
| 767 | fun main(args: Array<String>) { |
| 768 | val time = measureTimeMillis { |
| 769 | // we can initiate async actions outside of a coroutine |
Marcin Moskała | 7e94e70 | 2018-01-29 18:39:02 +0100 | [diff] [blame] | 770 | val one = somethingUsefulOneAsync() |
| 771 | val two = somethingUsefulTwoAsync() |
Roman Elizarov | 32d9532 | 2017-02-09 15:57:31 +0300 | [diff] [blame] | 772 | // but waiting for a result must involve either suspending or blocking. |
| 773 | // here we use `runBlocking { ... }` to block the main thread while waiting for the result |
| 774 | runBlocking { |
| 775 | println("The answer is ${one.await() + two.await()}") |
| 776 | } |
| 777 | } |
| 778 | println("Completed in $time ms") |
| 779 | } |
| 780 | ``` |
| 781 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 782 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-compose-04.kt) |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 783 | |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 784 | <!--- TEST ARBITRARY_TIME |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 785 | The answer is 42 |
| 786 | Completed in 1085 ms |
| 787 | --> |
| 788 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 789 | ## Coroutine context and dispatchers |
| 790 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 791 | Coroutines always execute in some context which is represented by the value of |
| 792 | [CoroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/-coroutine-context/) |
| 793 | type, defined in the Kotlin standard library. |
| 794 | |
| 795 | The coroutine context is a set of various elements. The main elements are the [Job] of the coroutine, |
| 796 | which we've seen before, and its dispatcher, which is covered in this section. |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 797 | |
| 798 | ### Dispatchers and threads |
| 799 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 800 | Coroutine context includes a _coroutine dispatcher_ (see [CoroutineDispatcher]) that determines what thread or threads |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 801 | the corresponding coroutine uses for its execution. Coroutine dispatcher can confine coroutine execution |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 802 | to a specific thread, dispatch it to a thread pool, or let it run unconfined. |
| 803 | |
| 804 | All coroutines builders like [launch] and [async] accept an optional |
| 805 | [CoroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/-coroutine-context/) |
| 806 | parameter that can be used to explicitly specify the dispatcher for new coroutine and other context elements. |
| 807 | |
| 808 | Try the following example: |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 809 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 810 | <!--- INCLUDE |
| 811 | import kotlin.coroutines.experimental.* |
| 812 | --> |
| 813 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 814 | ```kotlin |
| 815 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 816 | val jobs = arrayListOf<Job>() |
| 817 | jobs += launch(Unconfined) { // not confined -- will work with main thread |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 818 | println(" 'Unconfined': I'm working in thread ${Thread.currentThread().name}") |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 819 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 820 | jobs += launch(coroutineContext) { // context of the parent, runBlocking coroutine |
| 821 | println("'coroutineContext': I'm working in thread ${Thread.currentThread().name}") |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 822 | } |
| 823 | jobs += launch(CommonPool) { // will get dispatched to ForkJoinPool.commonPool (or equivalent) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 824 | println(" 'CommonPool': I'm working in thread ${Thread.currentThread().name}") |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 825 | } |
| 826 | jobs += launch(newSingleThreadContext("MyOwnThread")) { // will get its own new thread |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 827 | println(" 'newSTC': I'm working in thread ${Thread.currentThread().name}") |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 828 | } |
| 829 | jobs.forEach { it.join() } |
| 830 | } |
| 831 | ``` |
| 832 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 833 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-01.kt) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 834 | |
| 835 | It produces the following output (maybe in different order): |
| 836 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 837 | ```text |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 838 | 'Unconfined': I'm working in thread main |
| 839 | 'CommonPool': I'm working in thread ForkJoinPool.commonPool-worker-1 |
| 840 | 'newSTC': I'm working in thread MyOwnThread |
| 841 | 'coroutineContext': I'm working in thread main |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 842 | ``` |
| 843 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 844 | <!--- TEST LINES_START_UNORDERED --> |
| 845 | |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 846 | The default dispatcher that we've used in previous sections is representend by [DefaultDispatcher], which |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 847 | is equal to [CommonPool] in the current implementation. So, `launch { ... }` is the same |
Charles Muchene | fa13beb | 2018-01-08 16:56:54 +0300 | [diff] [blame] | 848 | as `launch(DefaultDispatcher) { ... }`, which is the same as `launch(CommonPool) { ... }`. |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 849 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 850 | The difference between parent |
| 851 | [coroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/coroutine-context.html) and |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 852 | [Unconfined] context will be shown later. |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 853 | |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 854 | Note, that [newSingleThreadContext] creates a new thread, which is a very expensive resource. |
| 855 | In a real application it must be either released, when no longer needed, using [close][ThreadPoolDispatcher.close] |
| 856 | function, or stored in a top-level variable and reused throughout the application. |
| 857 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 858 | ### Unconfined vs confined dispatcher |
| 859 | |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 860 | The [Unconfined] coroutine dispatcher starts coroutine in the caller thread, but only until the |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 861 | first suspension point. After suspension it resumes in the thread that is fully determined by the |
| 862 | suspending function that was invoked. Unconfined dispatcher is appropriate when coroutine does not |
| 863 | consume CPU time nor updates any shared data (like UI) that is confined to a specific thread. |
| 864 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 865 | On the other side, |
| 866 | [coroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/coroutine-context.html) |
| 867 | property, that is available inside any coroutine, is a reference to a context of this particular coroutine. |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 868 | This way, a parent context can be inherited. The default dispatcher for [runBlocking] coroutine, in particular, |
| 869 | is confined to the invoker thread, so inheriting it has the effect of confining execution to |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 870 | this thread with a predictable FIFO scheduling. |
| 871 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 872 | <!--- INCLUDE |
| 873 | import kotlin.coroutines.experimental.* |
| 874 | --> |
| 875 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 876 | ```kotlin |
| 877 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 878 | val jobs = arrayListOf<Job>() |
| 879 | jobs += launch(Unconfined) { // not confined -- will work with main thread |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 880 | println(" 'Unconfined': I'm working in thread ${Thread.currentThread().name}") |
Roman Elizarov | d002162 | 2017-03-10 15:43:38 +0300 | [diff] [blame] | 881 | delay(500) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 882 | println(" 'Unconfined': After delay in thread ${Thread.currentThread().name}") |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 883 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 884 | jobs += launch(coroutineContext) { // context of the parent, runBlocking coroutine |
| 885 | println("'coroutineContext': I'm working in thread ${Thread.currentThread().name}") |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 886 | delay(1000) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 887 | println("'coroutineContext': After delay in thread ${Thread.currentThread().name}") |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 888 | } |
| 889 | jobs.forEach { it.join() } |
| 890 | } |
| 891 | ``` |
| 892 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 893 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-02.kt) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 894 | |
| 895 | Produces the output: |
| 896 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 897 | ```text |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 898 | 'Unconfined': I'm working in thread main |
| 899 | 'coroutineContext': I'm working in thread main |
| 900 | 'Unconfined': After delay in thread kotlinx.coroutines.DefaultExecutor |
| 901 | 'coroutineContext': After delay in thread main |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 902 | ``` |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 903 | |
| 904 | <!--- TEST LINES_START --> |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 905 | |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 906 | So, the coroutine that had inherited `coroutineContext` of `runBlocking {...}` continues to execute |
| 907 | in the `main` thread, while the unconfined one had resumed in the default executor thread that [delay] |
| 908 | function is using. |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 909 | |
| 910 | ### Debugging coroutines and threads |
| 911 | |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 912 | Coroutines can suspend on one thread and resume on another thread with [Unconfined] dispatcher or |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 913 | with a default multi-threaded dispatcher. Even with a single-threaded dispatcher it might be hard to |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 914 | figure out what coroutine was doing what, where, and when. The common approach to debugging applications with |
| 915 | threads is to print the thread name in the log file on each log statement. This feature is universally supported |
| 916 | by logging frameworks. When using coroutines, the thread name alone does not give much of a context, so |
| 917 | `kotlinx.coroutines` includes debugging facilities to make it easier. |
| 918 | |
| 919 | Run the following code with `-Dkotlinx.coroutines.debug` JVM option: |
| 920 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 921 | <!--- INCLUDE |
| 922 | import kotlin.coroutines.experimental.* |
| 923 | --> |
| 924 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 925 | ```kotlin |
| 926 | fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") |
| 927 | |
| 928 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 929 | val a = async(coroutineContext) { |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 930 | log("I'm computing a piece of the answer") |
| 931 | 6 |
| 932 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 933 | val b = async(coroutineContext) { |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 934 | log("I'm computing another piece of the answer") |
| 935 | 7 |
| 936 | } |
| 937 | log("The answer is ${a.await() * b.await()}") |
| 938 | } |
| 939 | ``` |
| 940 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 941 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-03.kt) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 942 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 943 | There are three coroutines. The main coroutine (#1) -- `runBlocking` one, |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 944 | and two coroutines computing deferred values `a` (#2) and `b` (#3). |
| 945 | They are all executing in the context of `runBlocking` and are confined to the main thread. |
| 946 | The output of this code is: |
| 947 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 948 | ```text |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 949 | [main @coroutine#2] I'm computing a piece of the answer |
| 950 | [main @coroutine#3] I'm computing another piece of the answer |
| 951 | [main @coroutine#1] The answer is 42 |
| 952 | ``` |
| 953 | |
Kirill Timofeev | a518696 | 2017-10-25 14:25:47 +0300 | [diff] [blame] | 954 | <!--- TEST FLEXIBLE_THREAD --> |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 955 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 956 | The `log` function prints the name of the thread in square brackets and you can see, that it is the `main` |
| 957 | thread, but the identifier of the currently executing coroutine is appended to it. This identifier |
| 958 | is consecutively assigned to all created coroutines when debugging mode is turned on. |
| 959 | |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 960 | You can read more about debugging facilities in the documentation for [newCoroutineContext] function. |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 961 | |
| 962 | ### Jumping between threads |
| 963 | |
| 964 | Run the following code with `-Dkotlinx.coroutines.debug` JVM option: |
| 965 | |
| 966 | ```kotlin |
| 967 | fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") |
| 968 | |
| 969 | fun main(args: Array<String>) { |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 970 | newSingleThreadContext("Ctx1").use { ctx1 -> |
| 971 | newSingleThreadContext("Ctx2").use { ctx2 -> |
| 972 | runBlocking(ctx1) { |
| 973 | log("Started in ctx1") |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 974 | withContext(ctx2) { |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 975 | log("Working in ctx2") |
| 976 | } |
| 977 | log("Back to ctx1") |
| 978 | } |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 979 | } |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 980 | } |
| 981 | } |
| 982 | ``` |
| 983 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 984 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-04.kt) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 985 | |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 986 | It demonstrates several new techniques. One is using [runBlocking] with an explicitly specified context, and |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 987 | the other one is using [withContext] function to change a context of a coroutine while still staying in the |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 988 | same coroutine as you can see in the output below: |
| 989 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 990 | ```text |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 991 | [Ctx1 @coroutine#1] Started in ctx1 |
| 992 | [Ctx2 @coroutine#1] Working in ctx2 |
| 993 | [Ctx1 @coroutine#1] Back to ctx1 |
| 994 | ``` |
| 995 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 996 | <!--- TEST --> |
| 997 | |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 998 | |
Artsiom Chapialiou | e185ed6 | 2018-06-03 19:34:22 -0400 | [diff] [blame^] | 999 | Note, that this example also uses `use` function from the Kotlin standard library to release threads that |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 1000 | are created with [newSingleThreadContext] when they are no longer needed. |
| 1001 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1002 | ### Job in the context |
| 1003 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1004 | The coroutine's [Job] is part of its context. The coroutine can retrieve it from its own context |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 1005 | using `coroutineContext[Job]` expression: |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1006 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1007 | <!--- INCLUDE |
| 1008 | import kotlin.coroutines.experimental.* |
| 1009 | --> |
| 1010 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1011 | ```kotlin |
| 1012 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 1013 | println("My job is ${coroutineContext[Job]}") |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1014 | } |
| 1015 | ``` |
| 1016 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1017 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-05.kt) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1018 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1019 | It produces something like that when running in [debug mode](#debugging-coroutines-and-threads): |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1020 | |
| 1021 | ``` |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1022 | My job is "coroutine#1":BlockingCoroutine{Active}@6d311334 |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1023 | ``` |
| 1024 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1025 | <!--- TEST lines.size == 1 && lines[0].startsWith("My job is \"coroutine#1\":BlockingCoroutine{Active}@") --> |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1026 | |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 1027 | So, [isActive][CoroutineScope.isActive] in [CoroutineScope] is just a convenient shortcut for |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1028 | `coroutineContext[Job]?.isActive == true`. |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1029 | |
| 1030 | ### Children of a coroutine |
| 1031 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1032 | When |
| 1033 | [coroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/coroutine-context.html) |
| 1034 | of a coroutine is used to launch another coroutine, |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 1035 | the [Job] of the new coroutine becomes |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1036 | a _child_ of the parent coroutine's job. When the parent coroutine is cancelled, all its children |
| 1037 | are recursively cancelled, too. |
| 1038 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1039 | <!--- INCLUDE |
| 1040 | import kotlin.coroutines.experimental.* |
| 1041 | --> |
| 1042 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1043 | ```kotlin |
| 1044 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1045 | // launch a coroutine to process some kind of incoming request |
| 1046 | val request = launch { |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1047 | // it spawns two other jobs, one with its separate context |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1048 | val job1 = launch { |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1049 | println("job1: I have my own context and execute independently!") |
| 1050 | delay(1000) |
| 1051 | println("job1: I am not affected by cancellation of the request") |
| 1052 | } |
| 1053 | // and the other inherits the parent context |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 1054 | val job2 = launch(coroutineContext) { |
Roman Elizarov | 74619c1 | 2017-11-09 10:32:15 +0300 | [diff] [blame] | 1055 | delay(100) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1056 | println("job2: I am a child of the request coroutine") |
| 1057 | delay(1000) |
| 1058 | println("job2: I will not execute this line if my parent request is cancelled") |
| 1059 | } |
| 1060 | // request completes when both its sub-jobs complete: |
| 1061 | job1.join() |
| 1062 | job2.join() |
| 1063 | } |
| 1064 | delay(500) |
| 1065 | request.cancel() // cancel processing of the request |
| 1066 | delay(1000) // delay a second to see what happens |
| 1067 | println("main: Who has survived request cancellation?") |
| 1068 | } |
| 1069 | ``` |
| 1070 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1071 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-06.kt) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1072 | |
| 1073 | The output of this code is: |
| 1074 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1075 | ```text |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1076 | job1: I have my own context and execute independently! |
| 1077 | job2: I am a child of the request coroutine |
| 1078 | job1: I am not affected by cancellation of the request |
| 1079 | main: Who has survived request cancellation? |
| 1080 | ``` |
| 1081 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1082 | <!--- TEST --> |
| 1083 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1084 | ### Combining contexts |
| 1085 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1086 | Coroutine contexts can be combined using `+` operator. The context on the right-hand side replaces relevant entries |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 1087 | of the context on the left-hand side. For example, a [Job] of the parent coroutine can be inherited, while |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1088 | its dispatcher replaced: |
| 1089 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1090 | <!--- INCLUDE |
| 1091 | import kotlin.coroutines.experimental.* |
| 1092 | --> |
| 1093 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1094 | ```kotlin |
| 1095 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1096 | // start a coroutine to process some kind of incoming request |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 1097 | val request = launch(coroutineContext) { // use the context of `runBlocking` |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1098 | // spawns CPU-intensive child job in CommonPool !!! |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 1099 | val job = launch(coroutineContext + CommonPool) { |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1100 | println("job: I am a child of the request coroutine, but with a different dispatcher") |
| 1101 | delay(1000) |
| 1102 | println("job: I will not execute this line if my parent request is cancelled") |
| 1103 | } |
| 1104 | job.join() // request completes when its sub-job completes |
| 1105 | } |
| 1106 | delay(500) |
| 1107 | request.cancel() // cancel processing of the request |
| 1108 | delay(1000) // delay a second to see what happens |
| 1109 | println("main: Who has survived request cancellation?") |
| 1110 | } |
| 1111 | ``` |
| 1112 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1113 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-07.kt) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1114 | |
| 1115 | The expected outcome of this code is: |
| 1116 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1117 | ```text |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1118 | job: I am a child of the request coroutine, but with a different dispatcher |
| 1119 | main: Who has survived request cancellation? |
| 1120 | ``` |
| 1121 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1122 | <!--- TEST --> |
| 1123 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1124 | ### Parental responsibilities |
| 1125 | |
| 1126 | A parent coroutine always waits for completion of all its children. Parent does not have to explicitly track |
Roman Elizarov | 8839673 | 2017-09-27 21:30:47 +0300 | [diff] [blame] | 1127 | all the children it launches and it does not have to use [Job.join] to wait for them at the end: |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1128 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1129 | <!--- INCLUDE |
| 1130 | import kotlin.coroutines.experimental.* |
| 1131 | --> |
| 1132 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1133 | ```kotlin |
| 1134 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1135 | // launch a coroutine to process some kind of incoming request |
| 1136 | val request = launch { |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1137 | repeat(3) { i -> // launch a few children jobs |
| 1138 | launch(coroutineContext) { |
| 1139 | delay((i + 1) * 200L) // variable delay 200ms, 400ms, 600ms |
| 1140 | println("Coroutine $i is done") |
| 1141 | } |
| 1142 | } |
| 1143 | println("request: I'm done and I don't explicitly join my children that are still active") |
| 1144 | } |
| 1145 | request.join() // wait for completion of the request, including all its children |
| 1146 | println("Now processing of the request is complete") |
| 1147 | } |
| 1148 | ``` |
| 1149 | |
| 1150 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-08.kt) |
| 1151 | |
| 1152 | The result is going to be: |
| 1153 | |
| 1154 | ```text |
| 1155 | request: I'm done and I don't explicitly join my children that are still active |
| 1156 | Coroutine 0 is done |
| 1157 | Coroutine 1 is done |
| 1158 | Coroutine 2 is done |
| 1159 | Now processing of the request is complete |
| 1160 | ``` |
| 1161 | |
| 1162 | <!--- TEST --> |
| 1163 | |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1164 | ### Naming coroutines for debugging |
| 1165 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1166 | Automatically assigned ids are good when coroutines log often and you just need to correlate log records |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1167 | coming from the same coroutine. However, when coroutine is tied to the processing of a specific request |
| 1168 | or doing some specific background task, it is better to name it explicitly for debugging purposes. |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1169 | [CoroutineName] context element serves the same function as a thread name. It'll get displayed in the thread name that |
| 1170 | is executing this coroutine when [debugging mode](#debugging-coroutines-and-threads) is turned on. |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1171 | |
| 1172 | The following example demonstrates this concept: |
| 1173 | |
| 1174 | ```kotlin |
| 1175 | fun log(msg: String) = println("[${Thread.currentThread().name}] $msg") |
| 1176 | |
| 1177 | fun main(args: Array<String>) = runBlocking(CoroutineName("main")) { |
| 1178 | log("Started main coroutine") |
| 1179 | // run two background value computations |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1180 | val v1 = async(CoroutineName("v1coroutine")) { |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1181 | delay(500) |
Roman Elizarov | 674efea | 2017-10-21 17:16:30 +0300 | [diff] [blame] | 1182 | log("Computing v1") |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1183 | 252 |
| 1184 | } |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1185 | val v2 = async(CoroutineName("v2coroutine")) { |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1186 | delay(1000) |
Roman Elizarov | 674efea | 2017-10-21 17:16:30 +0300 | [diff] [blame] | 1187 | log("Computing v2") |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1188 | 6 |
| 1189 | } |
| 1190 | log("The answer for v1 / v2 = ${v1.await() / v2.await()}") |
| 1191 | } |
| 1192 | ``` |
| 1193 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1194 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-09.kt) |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1195 | |
| 1196 | The output it produces with `-Dkotlinx.coroutines.debug` JVM option is similar to: |
| 1197 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1198 | ```text |
Roman Elizarov | 2f6d7c9 | 2017-02-03 15:16:07 +0300 | [diff] [blame] | 1199 | [main @main#1] Started main coroutine |
| 1200 | [ForkJoinPool.commonPool-worker-1 @v1coroutine#2] Computing v1 |
| 1201 | [ForkJoinPool.commonPool-worker-2 @v2coroutine#3] Computing v2 |
| 1202 | [main @main#1] The answer for v1 / v2 = 42 |
| 1203 | ``` |
Roman Elizarov | 1293ccd | 2017-02-01 18:49:54 +0300 | [diff] [blame] | 1204 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1205 | <!--- TEST FLEXIBLE_THREAD --> |
| 1206 | |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1207 | ### Cancellation via explicit job |
| 1208 | |
| 1209 | Let us put our knowledge about contexts, children and jobs together. Assume that our application has |
| 1210 | an object with a lifecycle, but that object is not a coroutine. For example, we are writing an Android application |
| 1211 | and launch various coroutines in the context of an Android activity to perform asynchronous operations to fetch |
| 1212 | and update data, do animations, etc. All of these coroutines must be cancelled when activity is destroyed |
| 1213 | to avoid memory leaks. |
| 1214 | |
| 1215 | We can manage a lifecycle of our coroutines by creating an instance of [Job] that is tied to |
Roman Elizarov | 8839673 | 2017-09-27 21:30:47 +0300 | [diff] [blame] | 1216 | the lifecycle of our activity. A job instance is created using [Job()] factory function |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 1217 | as the following example shows. For convenience, rather than using `launch(coroutineContext + job)` expression, |
| 1218 | we can write `launch(coroutineContext, parent = job)` to make explicit the fact that the parent job is being used. |
| 1219 | |
| 1220 | Now, a single invocation of [Job.cancel] cancels all the children we've launched. |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1221 | Moreover, [Job.join] waits for all of them to complete, so we can also use [cancelAndJoin] here in |
| 1222 | this example: |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1223 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1224 | <!--- INCLUDE |
| 1225 | import kotlin.coroutines.experimental.* |
| 1226 | --> |
| 1227 | |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1228 | ```kotlin |
| 1229 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1230 | val job = Job() // create a job object to manage our lifecycle |
| 1231 | // now launch ten coroutines for a demo, each working for a different time |
| 1232 | val coroutines = List(10) { i -> |
| 1233 | // they are all children of our job object |
Roman Elizarov | e8f694e | 2017-11-28 10:12:00 +0300 | [diff] [blame] | 1234 | launch(coroutineContext, parent = job) { // we use the context of main runBlocking thread, but with our parent job |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1235 | delay((i + 1) * 200L) // variable delay 200ms, 400ms, ... etc |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1236 | println("Coroutine $i is done") |
| 1237 | } |
| 1238 | } |
| 1239 | println("Launched ${coroutines.size} coroutines") |
| 1240 | delay(500L) // delay for half a second |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1241 | println("Cancelling the job!") |
| 1242 | job.cancelAndJoin() // cancel all our coroutines and wait for all of them to complete |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1243 | } |
| 1244 | ``` |
| 1245 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1246 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-context-10.kt) |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1247 | |
| 1248 | The output of this example is: |
| 1249 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1250 | ```text |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1251 | Launched 10 coroutines |
| 1252 | Coroutine 0 is done |
| 1253 | Coroutine 1 is done |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1254 | Cancelling the job! |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1255 | ``` |
| 1256 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1257 | <!--- TEST --> |
| 1258 | |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1259 | As you can see, only the first three coroutines had printed a message and the others were cancelled |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1260 | by a single invocation of `job.cancelAndJoin()`. So all we need to do in our hypothetical Android |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1261 | application is to create a parent job object when activity is created, use it for child coroutines, |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1262 | and cancel it when activity is destroyed. We cannot `join` them in the case of Android lifecycle, |
| 1263 | since it is synchronous, but this joining ability is useful when building backend services to ensure bounded |
| 1264 | resource usage. |
Roman Elizarov | 2fd7cb3 | 2017-02-11 23:18:59 +0300 | [diff] [blame] | 1265 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1266 | ## Channels |
Roman Elizarov | 7deefb8 | 2017-01-31 10:33:17 +0300 | [diff] [blame] | 1267 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1268 | Deferred values provide a convenient way to transfer a single value between coroutines. |
| 1269 | Channels provide a way to transfer a stream of values. |
| 1270 | |
| 1271 | <!--- INCLUDE .*/example-channel-([0-9]+).kt |
| 1272 | import kotlinx.coroutines.experimental.channels.* |
| 1273 | --> |
| 1274 | |
| 1275 | ### Channel basics |
| 1276 | |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 1277 | A [Channel] is conceptually very similar to `BlockingQueue`. One key difference is that |
| 1278 | instead of a blocking `put` operation it has a suspending [send][SendChannel.send], and instead of |
| 1279 | a blocking `take` operation it has a suspending [receive][ReceiveChannel.receive]. |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1280 | |
| 1281 | ```kotlin |
| 1282 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1283 | val channel = Channel<Int>() |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1284 | launch { |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1285 | // this might be heavy CPU-consuming computation or async logic, we'll just send five squares |
| 1286 | for (x in 1..5) channel.send(x * x) |
| 1287 | } |
| 1288 | // here we print five received integers: |
| 1289 | repeat(5) { println(channel.receive()) } |
| 1290 | println("Done!") |
| 1291 | } |
| 1292 | ``` |
| 1293 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1294 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-01.kt) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1295 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1296 | The output of this code is: |
| 1297 | |
| 1298 | ```text |
| 1299 | 1 |
| 1300 | 4 |
| 1301 | 9 |
| 1302 | 16 |
| 1303 | 25 |
| 1304 | Done! |
| 1305 | ``` |
| 1306 | |
| 1307 | <!--- TEST --> |
| 1308 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1309 | ### Closing and iteration over channels |
| 1310 | |
| 1311 | Unlike a queue, a channel can be closed to indicate that no more elements are coming. |
| 1312 | On the receiver side it is convenient to use a regular `for` loop to receive elements |
| 1313 | from the channel. |
| 1314 | |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 1315 | Conceptually, a [close][SendChannel.close] is like sending a special close token to the channel. |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1316 | The iteration stops as soon as this close token is received, so there is a guarantee |
| 1317 | that all previously sent elements before the close are received: |
| 1318 | |
| 1319 | ```kotlin |
| 1320 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1321 | val channel = Channel<Int>() |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1322 | launch { |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1323 | for (x in 1..5) channel.send(x * x) |
| 1324 | channel.close() // we're done sending |
| 1325 | } |
| 1326 | // here we print received values using `for` loop (until the channel is closed) |
| 1327 | for (y in channel) println(y) |
| 1328 | println("Done!") |
| 1329 | } |
| 1330 | ``` |
| 1331 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1332 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-02.kt) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1333 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1334 | <!--- TEST |
| 1335 | 1 |
| 1336 | 4 |
| 1337 | 9 |
| 1338 | 16 |
| 1339 | 25 |
| 1340 | Done! |
| 1341 | --> |
| 1342 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1343 | ### Building channel producers |
| 1344 | |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 1345 | The pattern where a coroutine is producing a sequence of elements is quite common. |
| 1346 | This is a part of _producer-consumer_ pattern that is often found in concurrent code. |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1347 | You could abstract such a producer into a function that takes channel as its parameter, but this goes contrary |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 1348 | to common sense that results must be returned from functions. |
| 1349 | |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 1350 | There is a convenience coroutine builder named [produce] that makes it easy to do it right on producer side, |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1351 | and an extension function [consumeEach], that replaces a `for` loop on the consumer side: |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1352 | |
| 1353 | ```kotlin |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1354 | fun produceSquares() = produce<Int> { |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1355 | for (x in 1..5) send(x * x) |
| 1356 | } |
| 1357 | |
| 1358 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1359 | val squares = produceSquares() |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 1360 | squares.consumeEach { println(it) } |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1361 | println("Done!") |
| 1362 | } |
| 1363 | ``` |
| 1364 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1365 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-03.kt) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1366 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1367 | <!--- TEST |
| 1368 | 1 |
| 1369 | 4 |
| 1370 | 9 |
| 1371 | 16 |
| 1372 | 25 |
| 1373 | Done! |
| 1374 | --> |
| 1375 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1376 | ### Pipelines |
| 1377 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1378 | A pipeline is a pattern where one coroutine is producing, possibly infinite, stream of values: |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1379 | |
| 1380 | ```kotlin |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1381 | fun produceNumbers() = produce<Int> { |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1382 | var x = 1 |
| 1383 | while (true) send(x++) // infinite stream of integers starting from 1 |
| 1384 | } |
| 1385 | ``` |
| 1386 | |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 1387 | And another coroutine or coroutines are consuming that stream, doing some processing, and producing some other results. |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1388 | In the below example the numbers are just squared: |
| 1389 | |
| 1390 | ```kotlin |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1391 | fun square(numbers: ReceiveChannel<Int>) = produce<Int> { |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1392 | for (x in numbers) send(x * x) |
| 1393 | } |
| 1394 | ``` |
| 1395 | |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 1396 | The main code starts and connects the whole pipeline: |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1397 | |
| 1398 | ```kotlin |
| 1399 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1400 | val numbers = produceNumbers() // produces integers from 1 and on |
| 1401 | val squares = square(numbers) // squares integers |
| 1402 | for (i in 1..5) println(squares.receive()) // print first five |
| 1403 | println("Done!") // we are done |
| 1404 | squares.cancel() // need to cancel these coroutines in a larger app |
| 1405 | numbers.cancel() |
| 1406 | } |
| 1407 | ``` |
| 1408 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1409 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-04.kt) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1410 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1411 | <!--- TEST |
| 1412 | 1 |
| 1413 | 4 |
| 1414 | 9 |
| 1415 | 16 |
| 1416 | 25 |
| 1417 | Done! |
| 1418 | --> |
| 1419 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1420 | We don't have to cancel these coroutines in this example app, because |
| 1421 | [coroutines are like daemon threads](#coroutines-are-like-daemon-threads), |
| 1422 | but in a larger app we'll need to stop our pipeline if we don't need it anymore. |
| 1423 | Alternatively, we could have run pipeline coroutines as |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1424 | [children of a main coroutine](#children-of-a-coroutine) as is demonstrated in the following example. |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1425 | |
| 1426 | ### Prime numbers with pipeline |
| 1427 | |
Cedric Beust | fa0b28f | 2017-02-07 07:07:25 -0800 | [diff] [blame] | 1428 | Let's take pipelines to the extreme with an example that generates prime numbers using a pipeline |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1429 | of coroutines. We start with an infinite sequence of numbers. This time we introduce an |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1430 | explicit `context` parameter and pass it to [produce] builder, |
| 1431 | so that caller can control where our coroutines run: |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1432 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1433 | <!--- INCLUDE |
| 1434 | import kotlin.coroutines.experimental.* |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1435 | --> |
| 1436 | |
| 1437 | ```kotlin |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 1438 | fun numbersFrom(context: CoroutineContext, start: Int) = produce<Int>(context) { |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1439 | var x = start |
| 1440 | while (true) send(x++) // infinite stream of integers from start |
| 1441 | } |
| 1442 | ``` |
| 1443 | |
| 1444 | The following pipeline stage filters an incoming stream of numbers, removing all the numbers |
| 1445 | that are divisible by the given prime number: |
| 1446 | |
| 1447 | ```kotlin |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 1448 | fun filter(context: CoroutineContext, numbers: ReceiveChannel<Int>, prime: Int) = produce<Int>(context) { |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1449 | for (x in numbers) if (x % prime != 0) send(x) |
| 1450 | } |
| 1451 | ``` |
| 1452 | |
| 1453 | Now we build our pipeline by starting a stream of numbers from 2, taking a prime number from the current channel, |
Roman Elizarov | 62500ba | 2017-02-09 18:55:40 +0300 | [diff] [blame] | 1454 | and launching new pipeline stage for each prime number found: |
| 1455 | |
| 1456 | ``` |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 1457 | numbersFrom(2) -> filter(2) -> filter(3) -> filter(5) -> filter(7) ... |
Roman Elizarov | 62500ba | 2017-02-09 18:55:40 +0300 | [diff] [blame] | 1458 | ``` |
| 1459 | |
| 1460 | The following example prints the first ten prime numbers, |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1461 | running the whole pipeline in the context of the main thread. Since all the coroutines are launched as |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1462 | children of the main [runBlocking] coroutine in its |
| 1463 | [coroutineContext](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/coroutine-context.html), |
| 1464 | we don't have to keep an explicit list of all the coroutines we have started. |
Roman Elizarov | 3e387b8 | 2017-12-04 13:49:11 +0300 | [diff] [blame] | 1465 | We use [cancelChildren][kotlin.coroutines.experimental.CoroutineContext.cancelChildren] |
| 1466 | extension function to cancel all the children coroutines. |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1467 | |
| 1468 | ```kotlin |
| 1469 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 1470 | var cur = numbersFrom(coroutineContext, 2) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1471 | for (i in 1..10) { |
| 1472 | val prime = cur.receive() |
| 1473 | println(prime) |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 1474 | cur = filter(coroutineContext, cur, prime) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1475 | } |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1476 | coroutineContext.cancelChildren() // cancel all children to let main finish |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1477 | } |
| 1478 | ``` |
| 1479 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1480 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-05.kt) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1481 | |
| 1482 | The output of this code is: |
| 1483 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1484 | ```text |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1485 | 2 |
| 1486 | 3 |
| 1487 | 5 |
| 1488 | 7 |
| 1489 | 11 |
| 1490 | 13 |
| 1491 | 17 |
| 1492 | 19 |
| 1493 | 23 |
| 1494 | 29 |
| 1495 | ``` |
| 1496 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1497 | <!--- TEST --> |
| 1498 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1499 | Note, that you can build the same pipeline using |
| 1500 | [`buildIterator`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/build-iterator.html) |
| 1501 | coroutine builder from the standard library. |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 1502 | Replace `produce` with `buildIterator`, `send` with `yield`, `receive` with `next`, |
Roman Elizarov | 62500ba | 2017-02-09 18:55:40 +0300 | [diff] [blame] | 1503 | `ReceiveChannel` with `Iterator`, and get rid of the context. You will not need `runBlocking` either. |
| 1504 | However, the benefit of a pipeline that uses channels as shown above is that it can actually use |
| 1505 | multiple CPU cores if you run it in [CommonPool] context. |
| 1506 | |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 1507 | Anyway, this is an extremely impractical way to find prime numbers. In practice, pipelines do involve some |
Roman Elizarov | 62500ba | 2017-02-09 18:55:40 +0300 | [diff] [blame] | 1508 | other suspending invocations (like asynchronous calls to remote services) and these pipelines cannot be |
| 1509 | built using `buildSeqeunce`/`buildIterator`, because they do not allow arbitrary suspension, unlike |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1510 | `produce`, which is fully asynchronous. |
Roman Elizarov | 62500ba | 2017-02-09 18:55:40 +0300 | [diff] [blame] | 1511 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1512 | ### Fan-out |
| 1513 | |
| 1514 | Multiple coroutines may receive from the same channel, distributing work between themselves. |
| 1515 | Let us start with a producer coroutine that is periodically producing integers |
| 1516 | (ten numbers per second): |
| 1517 | |
| 1518 | ```kotlin |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1519 | fun produceNumbers() = produce<Int> { |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1520 | var x = 1 // start from 1 |
| 1521 | while (true) { |
| 1522 | send(x++) // produce next |
| 1523 | delay(100) // wait 0.1s |
| 1524 | } |
| 1525 | } |
| 1526 | ``` |
| 1527 | |
| 1528 | Then we can have several processor coroutines. In this example, they just print their id and |
| 1529 | received number: |
| 1530 | |
| 1531 | ```kotlin |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1532 | fun launchProcessor(id: Int, channel: ReceiveChannel<Int>) = launch { |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 1533 | channel.consumeEach { |
| 1534 | println("Processor #$id received $it") |
Roman Elizarov | ec9384c | 2017-03-02 22:09:08 +0300 | [diff] [blame] | 1535 | } |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1536 | } |
| 1537 | ``` |
| 1538 | |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 1539 | Now let us launch five processors and let them work for almost a second. See what happens: |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1540 | |
| 1541 | ```kotlin |
| 1542 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1543 | val producer = produceNumbers() |
| 1544 | repeat(5) { launchProcessor(it, producer) } |
Roman Elizarov | 35d2c34 | 2017-07-20 14:54:39 +0300 | [diff] [blame] | 1545 | delay(950) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1546 | producer.cancel() // cancel producer coroutine and thus kill them all |
| 1547 | } |
| 1548 | ``` |
| 1549 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1550 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-06.kt) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1551 | |
| 1552 | The output will be similar to the the following one, albeit the processor ids that receive |
| 1553 | each specific integer may be different: |
| 1554 | |
| 1555 | ``` |
| 1556 | Processor #2 received 1 |
| 1557 | Processor #4 received 2 |
| 1558 | Processor #0 received 3 |
| 1559 | Processor #1 received 4 |
| 1560 | Processor #3 received 5 |
| 1561 | Processor #2 received 6 |
| 1562 | Processor #4 received 7 |
| 1563 | Processor #0 received 8 |
| 1564 | Processor #1 received 9 |
| 1565 | Processor #3 received 10 |
| 1566 | ``` |
| 1567 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1568 | <!--- TEST lines.size == 10 && lines.withIndex().all { (i, line) -> line.startsWith("Processor #") && line.endsWith(" received ${i + 1}") } --> |
| 1569 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1570 | Note, that cancelling a producer coroutine closes its channel, thus eventually terminating iteration |
| 1571 | over the channel that processor coroutines are doing. |
| 1572 | |
| 1573 | ### Fan-in |
| 1574 | |
| 1575 | Multiple coroutines may send to the same channel. |
| 1576 | For example, let us have a channel of strings, and a suspending function that |
| 1577 | repeatedly sends a specified string to this channel with a specified delay: |
| 1578 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1579 | <!--- INCLUDE |
| 1580 | import kotlin.coroutines.experimental.* |
| 1581 | --> |
| 1582 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1583 | ```kotlin |
| 1584 | suspend fun sendString(channel: SendChannel<String>, s: String, time: Long) { |
| 1585 | while (true) { |
| 1586 | delay(time) |
| 1587 | channel.send(s) |
| 1588 | } |
| 1589 | } |
| 1590 | ``` |
| 1591 | |
Cedric Beust | fa0b28f | 2017-02-07 07:07:25 -0800 | [diff] [blame] | 1592 | Now, let us see what happens if we launch a couple of coroutines sending strings |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1593 | (in this example we launch them in the context of the main thread as main coroutine's children): |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1594 | |
| 1595 | ```kotlin |
| 1596 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1597 | val channel = Channel<String>() |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 1598 | launch(coroutineContext) { sendString(channel, "foo", 200L) } |
| 1599 | launch(coroutineContext) { sendString(channel, "BAR!", 500L) } |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1600 | repeat(6) { // receive first six |
| 1601 | println(channel.receive()) |
| 1602 | } |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1603 | coroutineContext.cancelChildren() // cancel all children to let main finish |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1604 | } |
| 1605 | ``` |
| 1606 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1607 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-07.kt) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1608 | |
| 1609 | The output is: |
| 1610 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1611 | ```text |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1612 | foo |
| 1613 | foo |
| 1614 | BAR! |
| 1615 | foo |
| 1616 | foo |
| 1617 | BAR! |
| 1618 | ``` |
| 1619 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1620 | <!--- TEST --> |
| 1621 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1622 | ### Buffered channels |
| 1623 | |
| 1624 | The channels shown so far had no buffer. Unbuffered channels transfer elements when sender and receiver |
| 1625 | meet each other (aka rendezvous). If send is invoked first, then it is suspended until receive is invoked, |
| 1626 | if receive is invoked first, it is suspended until send is invoked. |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 1627 | |
Roman Elizarov | 8839673 | 2017-09-27 21:30:47 +0300 | [diff] [blame] | 1628 | Both [Channel()] factory function and [produce] builder take an optional `capacity` parameter to |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1629 | specify _buffer size_. Buffer allows senders to send multiple elements before suspending, |
| 1630 | similar to the `BlockingQueue` with a specified capacity, which blocks when buffer is full. |
| 1631 | |
| 1632 | Take a look at the behavior of the following code: |
| 1633 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1634 | <!--- INCLUDE |
| 1635 | import kotlin.coroutines.experimental.* |
| 1636 | --> |
| 1637 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1638 | ```kotlin |
| 1639 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1640 | val channel = Channel<Int>(4) // create buffered channel |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1641 | val sender = launch(coroutineContext) { // launch sender coroutine |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1642 | repeat(10) { |
| 1643 | println("Sending $it") // print before sending each element |
| 1644 | channel.send(it) // will suspend when buffer is full |
| 1645 | } |
| 1646 | } |
| 1647 | // don't receive anything... just wait.... |
| 1648 | delay(1000) |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1649 | sender.cancel() // cancel sender coroutine |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1650 | } |
| 1651 | ``` |
| 1652 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1653 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-08.kt) |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1654 | |
| 1655 | It prints "sending" _five_ times using a buffered channel with capacity of _four_: |
| 1656 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1657 | ```text |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1658 | Sending 0 |
| 1659 | Sending 1 |
| 1660 | Sending 2 |
| 1661 | Sending 3 |
| 1662 | Sending 4 |
| 1663 | ``` |
| 1664 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1665 | <!--- TEST --> |
| 1666 | |
Roman Elizarov | b7721cf | 2017-02-03 19:23:08 +0300 | [diff] [blame] | 1667 | The first four elements are added to the buffer and the sender suspends when trying to send the fifth one. |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 1668 | |
Roman Elizarov | b0517ba | 2017-02-27 14:03:14 +0300 | [diff] [blame] | 1669 | ### Channels are fair |
| 1670 | |
| 1671 | Send and receive operations to channels are _fair_ with respect to the order of their invocation from |
| 1672 | multiple coroutines. They are served in first-in first-out order, e.g. the first coroutine to invoke `receive` |
| 1673 | gets the element. In the following example two coroutines "ping" and "pong" are |
| 1674 | receiving the "ball" object from the shared "table" channel. |
| 1675 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1676 | <!--- INCLUDE |
| 1677 | import kotlin.coroutines.experimental.* |
| 1678 | --> |
| 1679 | |
Roman Elizarov | b0517ba | 2017-02-27 14:03:14 +0300 | [diff] [blame] | 1680 | ```kotlin |
| 1681 | data class Ball(var hits: Int) |
| 1682 | |
| 1683 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1684 | val table = Channel<Ball>() // a shared table |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 1685 | launch(coroutineContext) { player("ping", table) } |
| 1686 | launch(coroutineContext) { player("pong", table) } |
Roman Elizarov | b0517ba | 2017-02-27 14:03:14 +0300 | [diff] [blame] | 1687 | table.send(Ball(0)) // serve the ball |
| 1688 | delay(1000) // delay 1 second |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1689 | coroutineContext.cancelChildren() // game over, cancel them |
Roman Elizarov | b0517ba | 2017-02-27 14:03:14 +0300 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | suspend fun player(name: String, table: Channel<Ball>) { |
| 1693 | for (ball in table) { // receive the ball in a loop |
| 1694 | ball.hits++ |
| 1695 | println("$name $ball") |
Roman Elizarov | f526b13 | 2017-03-10 16:07:14 +0300 | [diff] [blame] | 1696 | delay(300) // wait a bit |
Roman Elizarov | b0517ba | 2017-02-27 14:03:14 +0300 | [diff] [blame] | 1697 | table.send(ball) // send the ball back |
| 1698 | } |
| 1699 | } |
| 1700 | ``` |
| 1701 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1702 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-channel-09.kt) |
Roman Elizarov | b0517ba | 2017-02-27 14:03:14 +0300 | [diff] [blame] | 1703 | |
| 1704 | The "ping" coroutine is started first, so it is the first one to receive the ball. Even though "ping" |
| 1705 | coroutine immediately starts receiving the ball again after sending it back to the table, the ball gets |
| 1706 | received by the "pong" coroutine, because it was already waiting for it: |
| 1707 | |
| 1708 | ```text |
| 1709 | ping Ball(hits=1) |
| 1710 | pong Ball(hits=2) |
| 1711 | ping Ball(hits=3) |
| 1712 | pong Ball(hits=4) |
Roman Elizarov | b0517ba | 2017-02-27 14:03:14 +0300 | [diff] [blame] | 1713 | ``` |
| 1714 | |
| 1715 | <!--- TEST --> |
| 1716 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1717 | Note, that sometimes channels may produce executions that look unfair due to the nature of the executor |
| 1718 | that is being used. See [this issue](https://github.com/Kotlin/kotlinx.coroutines/issues/111) for details. |
| 1719 | |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1720 | ## Shared mutable state and concurrency |
| 1721 | |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1722 | Coroutines can be executed concurrently using a multi-threaded dispatcher like the default [CommonPool]. It presents |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1723 | all the usual concurrency problems. The main problem being synchronization of access to **shared mutable state**. |
| 1724 | Some solutions to this problem in the land of coroutines are similar to the solutions in the multi-threaded world, |
| 1725 | but others are unique. |
| 1726 | |
| 1727 | ### The problem |
| 1728 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1729 | Let us launch a thousand coroutines all doing the same action thousand times (for a total of a million executions). |
| 1730 | We'll also measure their completion time for further comparisons: |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1731 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1732 | <!--- INCLUDE .*/example-sync-03.kt |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1733 | import java.util.concurrent.atomic.* |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1734 | --> |
| 1735 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1736 | <!--- INCLUDE .*/example-sync-06.kt |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1737 | import kotlinx.coroutines.experimental.sync.* |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1738 | --> |
| 1739 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1740 | <!--- INCLUDE .*/example-sync-07.kt |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1741 | import kotlinx.coroutines.experimental.channels.* |
| 1742 | --> |
| 1743 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 1744 | <!--- INCLUDE .*/example-sync-([0-9a-z]+).kt |
| 1745 | import kotlin.system.* |
| 1746 | import kotlin.coroutines.experimental.* |
| 1747 | --> |
| 1748 | |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1749 | ```kotlin |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1750 | suspend fun massiveRun(context: CoroutineContext, action: suspend () -> Unit) { |
| 1751 | val n = 1000 // number of coroutines to launch |
| 1752 | val k = 1000 // times an action is repeated by each coroutine |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1753 | val time = measureTimeMillis { |
| 1754 | val jobs = List(n) { |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1755 | launch(context) { |
| 1756 | repeat(k) { action() } |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1757 | } |
| 1758 | } |
| 1759 | jobs.forEach { it.join() } |
| 1760 | } |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1761 | println("Completed ${n * k} actions in $time ms") |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1762 | } |
| 1763 | ``` |
| 1764 | |
Roman Elizarov | 43e9011 | 2017-05-10 11:25:20 +0300 | [diff] [blame] | 1765 | <!--- INCLUDE .*/example-sync-([0-9a-z]+).kt --> |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1766 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1767 | We start with a very simple action that increments a shared mutable variable using |
| 1768 | multi-threaded [CommonPool] context. |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1769 | |
| 1770 | ```kotlin |
| 1771 | var counter = 0 |
| 1772 | |
| 1773 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1774 | massiveRun(CommonPool) { |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1775 | counter++ |
| 1776 | } |
| 1777 | println("Counter = $counter") |
| 1778 | } |
| 1779 | ``` |
| 1780 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1781 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-01.kt) |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1782 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1783 | <!--- TEST LINES_START |
| 1784 | Completed 1000000 actions in |
| 1785 | Counter = |
| 1786 | --> |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1787 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1788 | What does it print at the end? It is highly unlikely to ever print "Counter = 1000000", because a thousand coroutines |
| 1789 | increment the `counter` concurrently from multiple threads without any synchronization. |
| 1790 | |
Roman Elizarov | 43e9011 | 2017-05-10 11:25:20 +0300 | [diff] [blame] | 1791 | > Note: if you have an old system with 2 or fewer CPUs, then you _will_ consistently see 1000000, because |
| 1792 | `CommonPool` is running in only one thread in this case. To reproduce the problem you'll need to make the |
| 1793 | following change: |
| 1794 | |
| 1795 | ```kotlin |
| 1796 | val mtContext = newFixedThreadPoolContext(2, "mtPool") // explicitly define context with two threads |
| 1797 | var counter = 0 |
| 1798 | |
| 1799 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1800 | massiveRun(mtContext) { // use it instead of CommonPool in this sample and below |
| 1801 | counter++ |
| 1802 | } |
| 1803 | println("Counter = $counter") |
| 1804 | } |
| 1805 | ``` |
| 1806 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1807 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-01b.kt) |
Roman Elizarov | 43e9011 | 2017-05-10 11:25:20 +0300 | [diff] [blame] | 1808 | |
| 1809 | <!--- TEST LINES_START |
| 1810 | Completed 1000000 actions in |
| 1811 | Counter = |
| 1812 | --> |
| 1813 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1814 | ### Volatiles are of no help |
| 1815 | |
| 1816 | There is common misconception that making a variable `volatile` solves concurrency problem. Let us try it: |
| 1817 | |
| 1818 | ```kotlin |
| 1819 | @Volatile // in Kotlin `volatile` is an annotation |
| 1820 | var counter = 0 |
| 1821 | |
| 1822 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1823 | massiveRun(CommonPool) { |
| 1824 | counter++ |
| 1825 | } |
| 1826 | println("Counter = $counter") |
| 1827 | } |
| 1828 | ``` |
| 1829 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1830 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-02.kt) |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1831 | |
| 1832 | <!--- TEST LINES_START |
| 1833 | Completed 1000000 actions in |
| 1834 | Counter = |
| 1835 | --> |
| 1836 | |
| 1837 | This code works slower, but we still don't get "Counter = 1000000" at the end, because volatile variables guarantee |
| 1838 | linearizable (this is a technical term for "atomic") reads and writes to the corresponding variable, but |
| 1839 | do not provide atomicity of larger actions (increment in our case). |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1840 | |
| 1841 | ### Thread-safe data structures |
| 1842 | |
| 1843 | The general solution that works both for threads and for coroutines is to use a thread-safe (aka synchronized, |
| 1844 | linearizable, or atomic) data structure that provides all the necessarily synchronization for the corresponding |
| 1845 | operations that needs to be performed on a shared state. |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1846 | In the case of a simple counter we can use `AtomicInteger` class which has atomic `incrementAndGet` operations: |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1847 | |
| 1848 | ```kotlin |
| 1849 | var counter = AtomicInteger() |
| 1850 | |
| 1851 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1852 | massiveRun(CommonPool) { |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1853 | counter.incrementAndGet() |
| 1854 | } |
| 1855 | println("Counter = ${counter.get()}") |
| 1856 | } |
| 1857 | ``` |
| 1858 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1859 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-03.kt) |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1860 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1861 | <!--- TEST ARBITRARY_TIME |
| 1862 | Completed 1000000 actions in xxx ms |
| 1863 | Counter = 1000000 |
| 1864 | --> |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1865 | |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1866 | This is the fastest solution for this particular problem. It works for plain counters, collections, queues and other |
| 1867 | standard data structures and basic operations on them. However, it does not easily scale to complex |
| 1868 | state or to complex operations that do not have ready-to-use thread-safe implementations. |
| 1869 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1870 | ### Thread confinement fine-grained |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1871 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1872 | _Thread confinement_ is an approach to the problem of shared mutable state where all access to the particular shared |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1873 | state is confined to a single thread. It is typically used in UI applications, where all UI state is confined to |
| 1874 | the single event-dispatch/application thread. It is easy to apply with coroutines by using a |
| 1875 | single-threaded context: |
| 1876 | |
| 1877 | ```kotlin |
| 1878 | val counterContext = newSingleThreadContext("CounterContext") |
| 1879 | var counter = 0 |
| 1880 | |
| 1881 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1882 | massiveRun(CommonPool) { // run each coroutine in CommonPool |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 1883 | withContext(counterContext) { // but confine each increment to the single-threaded context |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1884 | counter++ |
| 1885 | } |
| 1886 | } |
| 1887 | println("Counter = $counter") |
| 1888 | } |
| 1889 | ``` |
| 1890 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1891 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-04.kt) |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1892 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1893 | <!--- TEST ARBITRARY_TIME |
| 1894 | Completed 1000000 actions in xxx ms |
| 1895 | Counter = 1000000 |
| 1896 | --> |
| 1897 | |
| 1898 | This code works very slowly, because it does _fine-grained_ thread-confinement. Each individual increment switches |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 1899 | from multi-threaded `CommonPool` context to the single-threaded context using [withContext] block. |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1900 | |
| 1901 | ### Thread confinement coarse-grained |
| 1902 | |
| 1903 | In practice, thread confinement is performed in large chunks, e.g. big pieces of state-updating business logic |
| 1904 | are confined to the single thread. The following example does it like that, running each coroutine in |
| 1905 | the single-threaded context to start with. |
| 1906 | |
| 1907 | ```kotlin |
| 1908 | val counterContext = newSingleThreadContext("CounterContext") |
| 1909 | var counter = 0 |
| 1910 | |
| 1911 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 1912 | massiveRun(counterContext) { // run each coroutine in the single-threaded context |
| 1913 | counter++ |
| 1914 | } |
| 1915 | println("Counter = $counter") |
| 1916 | } |
| 1917 | ``` |
| 1918 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1919 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-05.kt) |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1920 | |
| 1921 | <!--- TEST ARBITRARY_TIME |
| 1922 | Completed 1000000 actions in xxx ms |
| 1923 | Counter = 1000000 |
| 1924 | --> |
| 1925 | |
| 1926 | This now works much faster and produces correct result. |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1927 | |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1928 | ### Mutual exclusion |
| 1929 | |
| 1930 | Mutual exclusion solution to the problem is to protect all modifications of the shared state with a _critical section_ |
| 1931 | that is never executed concurrently. In a blocking world you'd typically use `synchronized` or `ReentrantLock` for that. |
| 1932 | Coroutine's alternative is called [Mutex]. It has [lock][Mutex.lock] and [unlock][Mutex.unlock] functions to |
| 1933 | delimit a critical section. The key difference is that `Mutex.lock` is a suspending function. It does not block a thread. |
| 1934 | |
Roman Elizarov | 8839673 | 2017-09-27 21:30:47 +0300 | [diff] [blame] | 1935 | There is also [withLock] extension function that conveniently represents |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1936 | `mutex.lock(); try { ... } finally { mutex.unlock() }` pattern: |
| 1937 | |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1938 | ```kotlin |
| 1939 | val mutex = Mutex() |
| 1940 | var counter = 0 |
| 1941 | |
| 1942 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1943 | massiveRun(CommonPool) { |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 1944 | mutex.withLock { |
| 1945 | counter++ |
| 1946 | } |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1947 | } |
| 1948 | println("Counter = $counter") |
| 1949 | } |
| 1950 | ``` |
| 1951 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 1952 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-06.kt) |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1953 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 1954 | <!--- TEST ARBITRARY_TIME |
| 1955 | Completed 1000000 actions in xxx ms |
| 1956 | Counter = 1000000 |
| 1957 | --> |
| 1958 | |
| 1959 | The locking in this example is fine-grained, so it pays the price. However, it is a good choice for some situations |
| 1960 | where you absolutely must modify some shared state periodically, but there is no natural thread that this state |
| 1961 | is confined to. |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 1962 | |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1963 | ### Actors |
| 1964 | |
| 1965 | An actor is a combination of a coroutine, the state that is confined and is encapsulated into this coroutine, |
| 1966 | and a channel to communicate with other coroutines. A simple actor can be written as a function, |
| 1967 | but an actor with a complex state is better suited for a class. |
| 1968 | |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 1969 | There is an [actor] coroutine builder that conveniently combines actor's mailbox channel into its |
| 1970 | scope to receive messages from and combines the send channel into the resulting job object, so that a |
| 1971 | single reference to the actor can be carried around as its handle. |
| 1972 | |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 1973 | The first step of using an actor is to define a class of messages that an actor is going to process. |
| 1974 | Kotlin's [sealed classes](https://kotlinlang.org/docs/reference/sealed-classes.html) are well suited for that purpose. |
| 1975 | We define `CounterMsg` sealed class with `IncCounter` message to increment a counter and `GetCounter` message |
| 1976 | to get its value. The later needs to send a response. A [CompletableDeferred] communication |
| 1977 | primitive, that represents a single value that will be known (communicated) in the future, |
| 1978 | is used here for that purpose. |
| 1979 | |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1980 | ```kotlin |
| 1981 | // Message types for counterActor |
| 1982 | sealed class CounterMsg |
| 1983 | object IncCounter : CounterMsg() // one-way message to increment counter |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 1984 | class GetCounter(val response: CompletableDeferred<Int>) : CounterMsg() // a request with reply |
| 1985 | ``` |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1986 | |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 1987 | Then we define a function that launches an actor using an [actor] coroutine builder: |
| 1988 | |
| 1989 | ```kotlin |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1990 | // This function launches a new counter actor |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 1991 | fun counterActor() = actor<CounterMsg> { |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1992 | var counter = 0 // actor state |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 1993 | for (msg in channel) { // iterate over incoming messages |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1994 | when (msg) { |
| 1995 | is IncCounter -> counter++ |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 1996 | is GetCounter -> msg.response.complete(counter) |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 1997 | } |
| 1998 | } |
| 1999 | } |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 2000 | ``` |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 2001 | |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 2002 | The main code is straightforward: |
| 2003 | |
| 2004 | ```kotlin |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 2005 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 2006 | val counter = counterActor() // create the actor |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 2007 | massiveRun(CommonPool) { |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 2008 | counter.send(IncCounter) |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 2009 | } |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 2010 | // send a message to get a counter value from an actor |
| 2011 | val response = CompletableDeferred<Int>() |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 2012 | counter.send(GetCounter(response)) |
Roman Elizarov | 256812a | 2017-07-22 01:00:30 +0300 | [diff] [blame] | 2013 | println("Counter = ${response.await()}") |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 2014 | counter.close() // shutdown the actor |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 2015 | } |
| 2016 | ``` |
| 2017 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 2018 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-sync-07.kt) |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 2019 | |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 2020 | <!--- TEST ARBITRARY_TIME |
| 2021 | Completed 1000000 actions in xxx ms |
| 2022 | Counter = 1000000 |
| 2023 | --> |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2024 | |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 2025 | It does not matter (for correctness) what context the actor itself is executed in. An actor is |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 2026 | a coroutine and a coroutine is executed sequentially, so confinement of the state to the specific coroutine |
| 2027 | works as a solution to the problem of shared mutable state. |
| 2028 | |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 2029 | Actor is more efficient than locking under load, because in this case it always has work to do and it does not |
| 2030 | have to switch to a different context at all. |
| 2031 | |
| 2032 | > Note, that an [actor] coroutine builder is a dual of [produce] coroutine builder. An actor is associated |
| 2033 | with the channel that it receives messages from, while a producer is associated with the channel that it |
| 2034 | sends elements to. |
Roman Elizarov | 1e45960 | 2017-02-27 11:05:17 +0300 | [diff] [blame] | 2035 | |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2036 | ## Select expression |
| 2037 | |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2038 | Select expression makes it possible to await multiple suspending functions simultaneously and _select_ |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2039 | the first one that becomes available. |
| 2040 | |
| 2041 | <!--- INCLUDE .*/example-select-([0-9]+).kt |
| 2042 | import kotlinx.coroutines.experimental.channels.* |
| 2043 | import kotlinx.coroutines.experimental.selects.* |
| 2044 | --> |
| 2045 | |
| 2046 | ### Selecting from channels |
| 2047 | |
Roman Elizarov | 5785720 | 2017-03-02 23:17:25 +0300 | [diff] [blame] | 2048 | Let us have two producers of strings: `fizz` and `buzz`. The `fizz` produces "Fizz" string every 300 ms: |
| 2049 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 2050 | <!--- INCLUDE |
| 2051 | import kotlinx.coroutines.experimental.* |
| 2052 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 5785720 | 2017-03-02 23:17:25 +0300 | [diff] [blame] | 2053 | --> |
| 2054 | |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2055 | ```kotlin |
Roman Elizarov | 5785720 | 2017-03-02 23:17:25 +0300 | [diff] [blame] | 2056 | fun fizz(context: CoroutineContext) = produce<String>(context) { |
| 2057 | while (true) { // sends "Fizz" every 300 ms |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2058 | delay(300) |
| 2059 | send("Fizz") |
| 2060 | } |
| 2061 | } |
| 2062 | ``` |
| 2063 | |
Roman Elizarov | 5785720 | 2017-03-02 23:17:25 +0300 | [diff] [blame] | 2064 | And the `buzz` produces "Buzz!" string every 500 ms: |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2065 | |
| 2066 | ```kotlin |
Roman Elizarov | 5785720 | 2017-03-02 23:17:25 +0300 | [diff] [blame] | 2067 | fun buzz(context: CoroutineContext) = produce<String>(context) { |
| 2068 | while (true) { // sends "Buzz!" every 500 ms |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2069 | delay(500) |
| 2070 | send("Buzz!") |
| 2071 | } |
| 2072 | } |
| 2073 | ``` |
| 2074 | |
| 2075 | Using [receive][ReceiveChannel.receive] suspending function we can receive _either_ from one channel or the |
| 2076 | other. But [select] expression allows us to receive from _both_ simultaneously using its |
Roman Elizarov | 8a5564d | 2017-09-06 18:48:22 +0300 | [diff] [blame] | 2077 | [onReceive][ReceiveChannel.onReceive] clauses: |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2078 | |
| 2079 | ```kotlin |
Roman Elizarov | 5785720 | 2017-03-02 23:17:25 +0300 | [diff] [blame] | 2080 | suspend fun selectFizzBuzz(fizz: ReceiveChannel<String>, buzz: ReceiveChannel<String>) { |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2081 | select<Unit> { // <Unit> means that this select expression does not produce any result |
| 2082 | fizz.onReceive { value -> // this is the first select clause |
| 2083 | println("fizz -> '$value'") |
| 2084 | } |
| 2085 | buzz.onReceive { value -> // this is the second select clause |
| 2086 | println("buzz -> '$value'") |
| 2087 | } |
| 2088 | } |
| 2089 | } |
| 2090 | ``` |
| 2091 | |
Roman Elizarov | 5785720 | 2017-03-02 23:17:25 +0300 | [diff] [blame] | 2092 | Let us run it all seven times: |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2093 | |
| 2094 | ```kotlin |
| 2095 | fun main(args: Array<String>) = runBlocking<Unit> { |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 2096 | val fizz = fizz(coroutineContext) |
| 2097 | val buzz = buzz(coroutineContext) |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2098 | repeat(7) { |
Roman Elizarov | 5785720 | 2017-03-02 23:17:25 +0300 | [diff] [blame] | 2099 | selectFizzBuzz(fizz, buzz) |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2100 | } |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 2101 | coroutineContext.cancelChildren() // cancel fizz & buzz coroutines |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2102 | } |
| 2103 | ``` |
| 2104 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 2105 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-select-01.kt) |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2106 | |
| 2107 | The result of this code is: |
| 2108 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2109 | ```text |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2110 | fizz -> 'Fizz' |
| 2111 | buzz -> 'Buzz!' |
| 2112 | fizz -> 'Fizz' |
| 2113 | fizz -> 'Fizz' |
| 2114 | buzz -> 'Buzz!' |
| 2115 | fizz -> 'Fizz' |
| 2116 | buzz -> 'Buzz!' |
| 2117 | ``` |
| 2118 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2119 | <!--- TEST --> |
| 2120 | |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2121 | ### Selecting on close |
| 2122 | |
Roman Elizarov | 8a5564d | 2017-09-06 18:48:22 +0300 | [diff] [blame] | 2123 | The [onReceive][ReceiveChannel.onReceive] clause in `select` fails when the channel is closed and the corresponding |
| 2124 | `select` throws an exception. We can use [onReceiveOrNull][ReceiveChannel.onReceiveOrNull] clause to perform a |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2125 | specific action when the channel is closed. The following example also shows that `select` is an expression that returns |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2126 | the result of its selected clause: |
| 2127 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 2128 | <!--- INCLUDE |
| 2129 | import kotlin.coroutines.experimental.* |
| 2130 | --> |
| 2131 | |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2132 | ```kotlin |
| 2133 | suspend fun selectAorB(a: ReceiveChannel<String>, b: ReceiveChannel<String>): String = |
| 2134 | select<String> { |
| 2135 | a.onReceiveOrNull { value -> |
| 2136 | if (value == null) |
| 2137 | "Channel 'a' is closed" |
| 2138 | else |
| 2139 | "a -> '$value'" |
| 2140 | } |
| 2141 | b.onReceiveOrNull { value -> |
| 2142 | if (value == null) |
| 2143 | "Channel 'b' is closed" |
| 2144 | else |
| 2145 | "b -> '$value'" |
| 2146 | } |
| 2147 | } |
| 2148 | ``` |
| 2149 | |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2150 | Let's use it with channel `a` that produces "Hello" string four times and |
| 2151 | channel `b` that produces "World" four times: |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2152 | |
| 2153 | ```kotlin |
| 2154 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 2155 | // we are using the context of the main thread in this example for predictability ... |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 2156 | val a = produce<String>(coroutineContext) { |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2157 | repeat(4) { send("Hello $it") } |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2158 | } |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 2159 | val b = produce<String>(coroutineContext) { |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2160 | repeat(4) { send("World $it") } |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2161 | } |
| 2162 | repeat(8) { // print first eight results |
| 2163 | println(selectAorB(a, b)) |
| 2164 | } |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 2165 | coroutineContext.cancelChildren() |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2166 | } |
| 2167 | ``` |
| 2168 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 2169 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-select-02.kt) |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2170 | |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2171 | The result of this code is quite interesting, so we'll analyze it in mode detail: |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2172 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2173 | ```text |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2174 | a -> 'Hello 0' |
| 2175 | a -> 'Hello 1' |
| 2176 | b -> 'World 0' |
| 2177 | a -> 'Hello 2' |
| 2178 | a -> 'Hello 3' |
| 2179 | b -> 'World 1' |
| 2180 | Channel 'a' is closed |
| 2181 | Channel 'a' is closed |
| 2182 | ``` |
| 2183 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2184 | <!--- TEST --> |
| 2185 | |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2186 | There are couple of observations to make out of it. |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2187 | |
| 2188 | First of all, `select` is _biased_ to the first clause. When several clauses are selectable at the same time, |
| 2189 | the first one among them gets selected. Here, both channels are constantly producing strings, so `a` channel, |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2190 | being the first clause in select, wins. However, because we are using unbuffered channel, the `a` gets suspended from |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2191 | time to time on its [send][SendChannel.send] invocation and gives a chance for `b` to send, too. |
| 2192 | |
Roman Elizarov | 8a5564d | 2017-09-06 18:48:22 +0300 | [diff] [blame] | 2193 | The second observation, is that [onReceiveOrNull][ReceiveChannel.onReceiveOrNull] gets immediately selected when the |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2194 | channel is already closed. |
| 2195 | |
| 2196 | ### Selecting to send |
| 2197 | |
Roman Elizarov | 8a5564d | 2017-09-06 18:48:22 +0300 | [diff] [blame] | 2198 | Select expression has [onSend][SendChannel.onSend] clause that can be used for a great good in combination |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2199 | with a biased nature of selection. |
| 2200 | |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2201 | Let us write an example of producer of integers that sends its values to a `side` channel when |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2202 | the consumers on its primary channel cannot keep up with it: |
| 2203 | |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 2204 | <!--- INCLUDE |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 2205 | import kotlin.coroutines.experimental.* |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 2206 | --> |
| 2207 | |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2208 | ```kotlin |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 2209 | fun produceNumbers(context: CoroutineContext, side: SendChannel<Int>) = produce<Int>(context) { |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2210 | for (num in 1..10) { // produce 10 numbers from 1 to 10 |
| 2211 | delay(100) // every 100 ms |
| 2212 | select<Unit> { |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2213 | onSend(num) {} // Send to the primary channel |
| 2214 | side.onSend(num) {} // or to the side channel |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | ``` |
| 2219 | |
| 2220 | Consumer is going to be quite slow, taking 250 ms to process each number: |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 2221 | |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2222 | ```kotlin |
| 2223 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 2224 | val side = Channel<Int>() // allocate side channel |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 2225 | launch(coroutineContext) { // this is a very fast consumer for the side channel |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 2226 | side.consumeEach { println("Side channel has $it") } |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2227 | } |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 2228 | produceNumbers(coroutineContext, side).consumeEach { |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 2229 | println("Consuming $it") |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2230 | delay(250) // let us digest the consumed number properly, do not hurry |
| 2231 | } |
| 2232 | println("Done consuming") |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 2233 | coroutineContext.cancelChildren() |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2234 | } |
| 2235 | ``` |
| 2236 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 2237 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-select-03.kt) |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2238 | |
| 2239 | So let us see what happens: |
| 2240 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2241 | ```text |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2242 | Consuming 1 |
| 2243 | Side channel has 2 |
| 2244 | Side channel has 3 |
| 2245 | Consuming 4 |
| 2246 | Side channel has 5 |
| 2247 | Side channel has 6 |
| 2248 | Consuming 7 |
| 2249 | Side channel has 8 |
| 2250 | Side channel has 9 |
| 2251 | Consuming 10 |
| 2252 | Done consuming |
| 2253 | ``` |
| 2254 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2255 | <!--- TEST --> |
| 2256 | |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2257 | ### Selecting deferred values |
| 2258 | |
Roman Elizarov | 8a5564d | 2017-09-06 18:48:22 +0300 | [diff] [blame] | 2259 | Deferred values can be selected using [onAwait][Deferred.onAwait] clause. |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2260 | Let us start with an async function that returns a deferred string value after |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2261 | a random delay: |
| 2262 | |
| 2263 | <!--- INCLUDE .*/example-select-04.kt |
| 2264 | import java.util.* |
| 2265 | --> |
| 2266 | |
| 2267 | ```kotlin |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 2268 | fun asyncString(time: Int) = async { |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2269 | delay(time.toLong()) |
| 2270 | "Waited for $time ms" |
| 2271 | } |
| 2272 | ``` |
| 2273 | |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2274 | Let us start a dozen of them with a random delay. |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2275 | |
| 2276 | ```kotlin |
| 2277 | fun asyncStringsList(): List<Deferred<String>> { |
| 2278 | val random = Random(3) |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2279 | return List(12) { asyncString(random.nextInt(1000)) } |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2280 | } |
| 2281 | ``` |
| 2282 | |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2283 | Now the main function awaits for the first of them to complete and counts the number of deferred values |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2284 | that are still active. Note, that we've used here the fact that `select` expression is a Kotlin DSL, |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2285 | so we can provide clauses for it using an arbitrary code. In this case we iterate over a list |
| 2286 | of deferred values to provide `onAwait` clause for each deferred value. |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2287 | |
| 2288 | ```kotlin |
| 2289 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 2290 | val list = asyncStringsList() |
| 2291 | val result = select<String> { |
| 2292 | list.withIndex().forEach { (index, deferred) -> |
| 2293 | deferred.onAwait { answer -> |
| 2294 | "Deferred $index produced answer '$answer'" |
| 2295 | } |
| 2296 | } |
| 2297 | } |
| 2298 | println(result) |
Roman Elizarov | 7c864d8 | 2017-02-27 10:17:50 +0300 | [diff] [blame] | 2299 | val countActive = list.count { it.isActive } |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2300 | println("$countActive coroutines are still active") |
| 2301 | } |
| 2302 | ``` |
| 2303 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 2304 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-select-04.kt) |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2305 | |
| 2306 | The output is: |
| 2307 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2308 | ```text |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2309 | Deferred 4 produced answer 'Waited for 128 ms' |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2310 | 11 coroutines are still active |
| 2311 | ``` |
| 2312 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2313 | <!--- TEST --> |
| 2314 | |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2315 | ### Switch over a channel of deferred values |
| 2316 | |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2317 | Let us write a channel producer function that consumes a channel of deferred string values, waits for each received |
| 2318 | deferred value, but only until the next deferred value comes over or the channel is closed. This example puts together |
Roman Elizarov | 8a5564d | 2017-09-06 18:48:22 +0300 | [diff] [blame] | 2319 | [onReceiveOrNull][ReceiveChannel.onReceiveOrNull] and [onAwait][Deferred.onAwait] clauses in the same `select`: |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2320 | |
Roman Elizarov | 9fe5f46 | 2018-02-21 19:05:52 +0300 | [diff] [blame] | 2321 | <!--- INCLUDE |
| 2322 | import kotlin.coroutines.experimental.* |
| 2323 | --> |
| 2324 | |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2325 | ```kotlin |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 2326 | fun switchMapDeferreds(input: ReceiveChannel<Deferred<String>>) = produce<String> { |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2327 | var current = input.receive() // start with first received deferred value |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2328 | while (isActive) { // loop while not cancelled/closed |
| 2329 | val next = select<Deferred<String>?> { // return next deferred value from this select or null |
| 2330 | input.onReceiveOrNull { update -> |
| 2331 | update // replaces next value to wait |
| 2332 | } |
| 2333 | current.onAwait { value -> |
| 2334 | send(value) // send value that current deferred has produced |
| 2335 | input.receiveOrNull() // and use the next deferred from the input channel |
| 2336 | } |
| 2337 | } |
| 2338 | if (next == null) { |
| 2339 | println("Channel was closed") |
| 2340 | break // out of loop |
| 2341 | } else { |
| 2342 | current = next |
| 2343 | } |
| 2344 | } |
| 2345 | } |
| 2346 | ``` |
| 2347 | |
| 2348 | To test it, we'll use a simple async function that resolves to a specified string after a specified time: |
| 2349 | |
| 2350 | ```kotlin |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 2351 | fun asyncString(str: String, time: Long) = async { |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2352 | delay(time) |
| 2353 | str |
| 2354 | } |
| 2355 | ``` |
| 2356 | |
| 2357 | The main function just launches a coroutine to print results of `switchMapDeferreds` and sends some test |
| 2358 | data to it: |
| 2359 | |
| 2360 | ```kotlin |
| 2361 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 2362 | val chan = Channel<Deferred<String>>() // the channel for test |
Roman Elizarov | 43e3af7 | 2017-07-21 16:01:31 +0300 | [diff] [blame] | 2363 | launch(coroutineContext) { // launch printing coroutine |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2364 | for (s in switchMapDeferreds(chan)) |
| 2365 | println(s) // print each received string |
| 2366 | } |
| 2367 | chan.send(asyncString("BEGIN", 100)) |
| 2368 | delay(200) // enough time for "BEGIN" to be produced |
| 2369 | chan.send(asyncString("Slow", 500)) |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2370 | delay(100) // not enough time to produce slow |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2371 | chan.send(asyncString("Replace", 100)) |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2372 | delay(500) // give it time before the last one |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2373 | chan.send(asyncString("END", 500)) |
| 2374 | delay(1000) // give it time to process |
Roman Elizarov | a84730b | 2017-02-22 11:58:50 +0300 | [diff] [blame] | 2375 | chan.close() // close the channel ... |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2376 | delay(500) // and wait some time to let it finish |
| 2377 | } |
| 2378 | ``` |
| 2379 | |
Roman Elizarov | e8d7934 | 2017-08-29 15:21:21 +0300 | [diff] [blame] | 2380 | > You can get full code [here](core/kotlinx-coroutines-core/src/test/kotlin/guide/example-select-05.kt) |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2381 | |
| 2382 | The result of this code: |
| 2383 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2384 | ```text |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2385 | BEGIN |
| 2386 | Replace |
| 2387 | END |
| 2388 | Channel was closed |
| 2389 | ``` |
| 2390 | |
Roman Elizarov | 731f0ad | 2017-02-22 20:48:45 +0300 | [diff] [blame] | 2391 | <!--- TEST --> |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2392 | |
Roman Elizarov | 8db1733 | 2017-03-09 12:40:45 +0300 | [diff] [blame] | 2393 | ## Further reading |
| 2394 | |
| 2395 | * [Guide to UI programming with coroutines](ui/coroutines-guide-ui.md) |
Roman Elizarov | 8a4a8e1 | 2017-03-09 19:52:58 +0300 | [diff] [blame] | 2396 | * [Guide to reactive streams with coroutines](reactive/coroutines-guide-reactive.md) |
Roman Elizarov | 8db1733 | 2017-03-09 12:40:45 +0300 | [diff] [blame] | 2397 | * [Coroutines design document (KEEP)](https://github.com/Kotlin/kotlin-coroutines/blob/master/kotlin-coroutines-informal.md) |
| 2398 | * [Full kotlinx.coroutines API reference](http://kotlin.github.io/kotlinx.coroutines) |
| 2399 | |
Roman Elizarov | e7e2ad1 | 2017-05-17 14:47:31 +0300 | [diff] [blame] | 2400 | <!--- MODULE kotlinx-coroutines-core --> |
Roman Elizarov | e0c817d | 2017-02-10 10:22:01 +0300 | [diff] [blame] | 2401 | <!--- INDEX kotlinx.coroutines.experimental --> |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2402 | [launch]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/launch.html |
| 2403 | [delay]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/delay.html |
| 2404 | [runBlocking]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/run-blocking.html |
Roman Elizarov | e82dee7 | 2017-08-18 16:49:09 +0300 | [diff] [blame] | 2405 | [Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/index.html |
Roman Elizarov | 8b38fa2 | 2017-09-27 17:44:31 +0300 | [diff] [blame] | 2406 | [cancelAndJoin]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/cancel-and-join.html |
Roman Elizarov | 8839673 | 2017-09-27 21:30:47 +0300 | [diff] [blame] | 2407 | [Job.cancel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/cancel.html |
| 2408 | [Job.join]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/join.html |
Roman Elizarov | cbb602d | 2017-12-23 14:24:26 +0300 | [diff] [blame] | 2409 | [CancellationException]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-cancellation-exception/index.html |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2410 | [yield]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/yield.html |
Roman Elizarov | bff3f37 | 2017-03-01 18:12:27 +0300 | [diff] [blame] | 2411 | [CoroutineScope.isActive]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope/is-active.html |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2412 | [CoroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope/index.html |
Roman Elizarov | f9e13f5 | 2017-12-21 12:23:15 +0300 | [diff] [blame] | 2413 | [withContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/with-context.html |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2414 | [NonCancellable]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-non-cancellable/index.html |
| 2415 | [withTimeout]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/with-timeout.html |
Roman Elizarov | 63f6ea2 | 2017-09-06 18:42:34 +0300 | [diff] [blame] | 2416 | [withTimeoutOrNull]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/with-timeout-or-null.html |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2417 | [async]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/async.html |
| 2418 | [Deferred]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-deferred/index.html |
Roman Elizarov | ecda27f | 2017-04-06 23:06:26 +0300 | [diff] [blame] | 2419 | [CoroutineStart.LAZY]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-start/-l-a-z-y.html |
Roman Elizarov | bff3f37 | 2017-03-01 18:12:27 +0300 | [diff] [blame] | 2420 | [Deferred.await]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-deferred/await.html |
| 2421 | [Job.start]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/start.html |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 2422 | [CoroutineDispatcher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-dispatcher/index.html |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 2423 | [DefaultDispatcher]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-default-dispatcher.html |
Roman Elizarov | 66f018c | 2017-09-29 21:39:03 +0300 | [diff] [blame] | 2424 | [CommonPool]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-common-pool/index.html |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 2425 | [Unconfined]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-unconfined/index.html |
Roman Elizarov | d9ae2bc | 2017-10-20 17:36:56 +0800 | [diff] [blame] | 2426 | [newSingleThreadContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-single-thread-context.html |
| 2427 | [ThreadPoolDispatcher.close]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-thread-pool-dispatcher/close.html |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 2428 | [newCoroutineContext]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/new-coroutine-context.html |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2429 | [CoroutineName]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-name/index.html |
Roman Elizarov | 8839673 | 2017-09-27 21:30:47 +0300 | [diff] [blame] | 2430 | [Job()]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job.html |
Roman Elizarov | 3e387b8 | 2017-12-04 13:49:11 +0300 | [diff] [blame] | 2431 | [kotlin.coroutines.experimental.CoroutineContext.cancelChildren]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/kotlin.coroutines.experimental.-coroutine-context/cancel-children.html |
Roman Elizarov | e82dee7 | 2017-08-18 16:49:09 +0300 | [diff] [blame] | 2432 | [CompletableDeferred]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-completable-deferred/index.html |
Roman Elizarov | 8a5564d | 2017-09-06 18:48:22 +0300 | [diff] [blame] | 2433 | [Deferred.onAwait]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-deferred/on-await.html |
Roman Elizarov | f5bc047 | 2017-02-22 11:38:13 +0300 | [diff] [blame] | 2434 | <!--- INDEX kotlinx.coroutines.experimental.sync --> |
Roman Elizarov | e82dee7 | 2017-08-18 16:49:09 +0300 | [diff] [blame] | 2435 | [Mutex]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/index.html |
Roman Elizarov | bff3f37 | 2017-03-01 18:12:27 +0300 | [diff] [blame] | 2436 | [Mutex.lock]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/lock.html |
| 2437 | [Mutex.unlock]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/-mutex/unlock.html |
Roman Elizarov | 8839673 | 2017-09-27 21:30:47 +0300 | [diff] [blame] | 2438 | [withLock]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.sync/with-lock.html |
Roman Elizarov | e0c817d | 2017-02-10 10:22:01 +0300 | [diff] [blame] | 2439 | <!--- INDEX kotlinx.coroutines.experimental.channels --> |
Roman Elizarov | e82dee7 | 2017-08-18 16:49:09 +0300 | [diff] [blame] | 2440 | [Channel]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel/index.html |
Roman Elizarov | bff3f37 | 2017-03-01 18:12:27 +0300 | [diff] [blame] | 2441 | [SendChannel.send]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/send.html |
| 2442 | [ReceiveChannel.receive]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/receive.html |
| 2443 | [SendChannel.close]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/close.html |
Roman Elizarov | a5e653f | 2017-02-13 13:49:55 +0300 | [diff] [blame] | 2444 | [produce]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/produce.html |
Roman Elizarov | 86349be | 2017-03-17 16:47:37 +0300 | [diff] [blame] | 2445 | [consumeEach]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/consume-each.html |
Roman Elizarov | 8839673 | 2017-09-27 21:30:47 +0300 | [diff] [blame] | 2446 | [Channel()]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-channel.html |
Roman Elizarov | c0e19f8 | 2017-02-27 11:59:14 +0300 | [diff] [blame] | 2447 | [actor]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/actor.html |
Roman Elizarov | 8a5564d | 2017-09-06 18:48:22 +0300 | [diff] [blame] | 2448 | [ReceiveChannel.onReceive]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/on-receive.html |
| 2449 | [ReceiveChannel.onReceiveOrNull]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-receive-channel/on-receive-or-null.html |
| 2450 | [SendChannel.onSend]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.channels/-send-channel/on-send.html |
Roman Elizarov | d4dcbe2 | 2017-02-22 09:57:46 +0300 | [diff] [blame] | 2451 | <!--- INDEX kotlinx.coroutines.experimental.selects --> |
| 2452 | [select]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental.selects/select.html |
Roman Elizarov | 419a6c8 | 2017-02-09 18:36:22 +0300 | [diff] [blame] | 2453 | <!--- END --> |