hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 1 | <!--- INCLUDE .*/example-([a-z]+)-([0-9a-z]+)\.kt |
| 2 | /* |
| 3 | * Copyright 2016-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 4 | */ |
| 5 | |
| 6 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame^] | 7 | package kotlinx.coroutines.guide.$$1$$2 |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 8 | |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame^] | 9 | import kotlinx.coroutines.* |
| 10 | import kotlinx.coroutines.channels.* |
| 11 | import kotlinx.coroutines.selects.* |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 12 | --> |
| 13 | <!--- KNIT ../core/kotlinx-coroutines-core/test/guide/.*\.kt --> |
| 14 | <!--- TEST_OUT ../core/kotlinx-coroutines-core/test/guide/test/SelectGuideTest.kt |
| 15 | // This file was automatically generated from coroutines-guide.md by Knit tool. Do not edit. |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame^] | 16 | package kotlinx.coroutines.guide.test |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 17 | |
| 18 | import org.junit.Test |
| 19 | |
| 20 | class SelectGuideTest { |
| 21 | --> |
| 22 | |
| 23 | |
| 24 | ## Table of contents |
| 25 | |
| 26 | <!--- TOC --> |
| 27 | |
Vsevolod Tolstopyatov | b590aa3 | 2018-09-27 18:34:05 +0300 | [diff] [blame] | 28 | * [Select expression (experimental)](#select-expression-experimental) |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 29 | * [Selecting from channels](#selecting-from-channels) |
| 30 | * [Selecting on close](#selecting-on-close) |
| 31 | * [Selecting to send](#selecting-to-send) |
| 32 | * [Selecting deferred values](#selecting-deferred-values) |
| 33 | * [Switch over a channel of deferred values](#switch-over-a-channel-of-deferred-values) |
| 34 | |
| 35 | <!--- END_TOC --> |
| 36 | |
| 37 | |
| 38 | |
| 39 | ## Select expression (experimental) |
| 40 | |
| 41 | Select expression makes it possible to await multiple suspending functions simultaneously and _select_ |
| 42 | the first one that becomes available. |
| 43 | |
| 44 | > Select expressions are an experimental feature of `kotlinx.coroutines`. Their API is expected to |
| 45 | evolve in the upcoming updates of the `kotlinx.coroutines` library with potentially |
| 46 | breaking changes. |
| 47 | |
| 48 | ### Selecting from channels |
| 49 | |
| 50 | Let us have two producers of strings: `fizz` and `buzz`. The `fizz` produces "Fizz" string every 300 ms: |
| 51 | |
| 52 | <!--- INCLUDE |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame^] | 53 | import kotlinx.coroutines.* |
| 54 | import kotlin.coroutines.* |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 55 | --> |
| 56 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 57 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 58 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 59 | ```kotlin |
| 60 | fun CoroutineScope.fizz() = produce<String> { |
| 61 | while (true) { // sends "Fizz" every 300 ms |
| 62 | delay(300) |
| 63 | send("Fizz") |
| 64 | } |
| 65 | } |
| 66 | ``` |
| 67 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 68 | </div> |
| 69 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 70 | And the `buzz` produces "Buzz!" string every 500 ms: |
| 71 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 72 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 73 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 74 | ```kotlin |
| 75 | fun CoroutineScope.buzz() = produce<String> { |
| 76 | while (true) { // sends "Buzz!" every 500 ms |
| 77 | delay(500) |
| 78 | send("Buzz!") |
| 79 | } |
| 80 | } |
| 81 | ``` |
| 82 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 83 | </div> |
| 84 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 85 | Using [receive][ReceiveChannel.receive] suspending function we can receive _either_ from one channel or the |
| 86 | other. But [select] expression allows us to receive from _both_ simultaneously using its |
| 87 | [onReceive][ReceiveChannel.onReceive] clauses: |
| 88 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 89 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 90 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 91 | ```kotlin |
| 92 | suspend fun selectFizzBuzz(fizz: ReceiveChannel<String>, buzz: ReceiveChannel<String>) { |
| 93 | select<Unit> { // <Unit> means that this select expression does not produce any result |
| 94 | fizz.onReceive { value -> // this is the first select clause |
| 95 | println("fizz -> '$value'") |
| 96 | } |
| 97 | buzz.onReceive { value -> // this is the second select clause |
| 98 | println("buzz -> '$value'") |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | ``` |
| 103 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 104 | </div> |
| 105 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 106 | Let us run it all seven times: |
| 107 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 108 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 109 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 110 | ```kotlin |
| 111 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 112 | val fizz = fizz() |
| 113 | val buzz = buzz() |
| 114 | repeat(7) { |
| 115 | selectFizzBuzz(fizz, buzz) |
| 116 | } |
| 117 | coroutineContext.cancelChildren() // cancel fizz & buzz coroutines |
| 118 | } |
| 119 | ``` |
| 120 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 121 | </div> |
| 122 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 123 | > You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-select-01.kt) |
| 124 | |
| 125 | The result of this code is: |
| 126 | |
| 127 | ```text |
| 128 | fizz -> 'Fizz' |
| 129 | buzz -> 'Buzz!' |
| 130 | fizz -> 'Fizz' |
| 131 | fizz -> 'Fizz' |
| 132 | buzz -> 'Buzz!' |
| 133 | fizz -> 'Fizz' |
| 134 | buzz -> 'Buzz!' |
| 135 | ``` |
| 136 | |
| 137 | <!--- TEST --> |
| 138 | |
| 139 | ### Selecting on close |
| 140 | |
| 141 | The [onReceive][ReceiveChannel.onReceive] clause in `select` fails when the channel is closed causing the corresponding |
| 142 | `select` to throw an exception. We can use [onReceiveOrNull][ReceiveChannel.onReceiveOrNull] clause to perform a |
| 143 | specific action when the channel is closed. The following example also shows that `select` is an expression that returns |
| 144 | the result of its selected clause: |
| 145 | |
| 146 | <!--- INCLUDE |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame^] | 147 | import kotlin.coroutines.* |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 148 | --> |
| 149 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 150 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 151 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 152 | ```kotlin |
| 153 | suspend fun selectAorB(a: ReceiveChannel<String>, b: ReceiveChannel<String>): String = |
| 154 | select<String> { |
| 155 | a.onReceiveOrNull { value -> |
| 156 | if (value == null) |
| 157 | "Channel 'a' is closed" |
| 158 | else |
| 159 | "a -> '$value'" |
| 160 | } |
| 161 | b.onReceiveOrNull { value -> |
| 162 | if (value == null) |
| 163 | "Channel 'b' is closed" |
| 164 | else |
| 165 | "b -> '$value'" |
| 166 | } |
| 167 | } |
| 168 | ``` |
| 169 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 170 | </div> |
| 171 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 172 | Let's use it with channel `a` that produces "Hello" string four times and |
| 173 | channel `b` that produces "World" four times: |
| 174 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 175 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 176 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 177 | ```kotlin |
| 178 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 179 | val a = produce<String> { |
| 180 | repeat(4) { send("Hello $it") } |
| 181 | } |
| 182 | val b = produce<String> { |
| 183 | repeat(4) { send("World $it") } |
| 184 | } |
| 185 | repeat(8) { // print first eight results |
| 186 | println(selectAorB(a, b)) |
| 187 | } |
| 188 | coroutineContext.cancelChildren() |
| 189 | } |
| 190 | ``` |
| 191 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 192 | </div> |
| 193 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 194 | > You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-select-02.kt) |
| 195 | |
| 196 | The result of this code is quite interesting, so we'll analyze it in mode detail: |
| 197 | |
| 198 | ```text |
| 199 | a -> 'Hello 0' |
| 200 | a -> 'Hello 1' |
| 201 | b -> 'World 0' |
| 202 | a -> 'Hello 2' |
| 203 | a -> 'Hello 3' |
| 204 | b -> 'World 1' |
| 205 | Channel 'a' is closed |
| 206 | Channel 'a' is closed |
| 207 | ``` |
| 208 | |
| 209 | <!--- TEST --> |
| 210 | |
| 211 | There are couple of observations to make out of it. |
| 212 | |
| 213 | First of all, `select` is _biased_ to the first clause. When several clauses are selectable at the same time, |
| 214 | the first one among them gets selected. Here, both channels are constantly producing strings, so `a` channel, |
| 215 | being the first clause in select, wins. However, because we are using unbuffered channel, the `a` gets suspended from |
| 216 | time to time on its [send][SendChannel.send] invocation and gives a chance for `b` to send, too. |
| 217 | |
| 218 | The second observation, is that [onReceiveOrNull][ReceiveChannel.onReceiveOrNull] gets immediately selected when the |
| 219 | channel is already closed. |
| 220 | |
| 221 | ### Selecting to send |
| 222 | |
| 223 | Select expression has [onSend][SendChannel.onSend] clause that can be used for a great good in combination |
| 224 | with a biased nature of selection. |
| 225 | |
| 226 | Let us write an example of producer of integers that sends its values to a `side` channel when |
| 227 | the consumers on its primary channel cannot keep up with it: |
| 228 | |
| 229 | <!--- INCLUDE |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame^] | 230 | import kotlin.coroutines.* |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 231 | --> |
| 232 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 233 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 234 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 235 | ```kotlin |
| 236 | fun CoroutineScope.produceNumbers(side: SendChannel<Int>) = produce<Int> { |
| 237 | for (num in 1..10) { // produce 10 numbers from 1 to 10 |
| 238 | delay(100) // every 100 ms |
| 239 | select<Unit> { |
| 240 | onSend(num) {} // Send to the primary channel |
| 241 | side.onSend(num) {} // or to the side channel |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | ``` |
| 246 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 247 | </div> |
| 248 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 249 | Consumer is going to be quite slow, taking 250 ms to process each number: |
| 250 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 251 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 252 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 253 | ```kotlin |
| 254 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 255 | val side = Channel<Int>() // allocate side channel |
| 256 | launch { // this is a very fast consumer for the side channel |
| 257 | side.consumeEach { println("Side channel has $it") } |
| 258 | } |
| 259 | produceNumbers(side).consumeEach { |
| 260 | println("Consuming $it") |
| 261 | delay(250) // let us digest the consumed number properly, do not hurry |
| 262 | } |
| 263 | println("Done consuming") |
| 264 | coroutineContext.cancelChildren() |
| 265 | } |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 266 | ``` |
| 267 | |
| 268 | </div> |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 269 | |
| 270 | > You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-select-03.kt) |
| 271 | |
| 272 | So let us see what happens: |
| 273 | |
| 274 | ```text |
| 275 | Consuming 1 |
| 276 | Side channel has 2 |
| 277 | Side channel has 3 |
| 278 | Consuming 4 |
| 279 | Side channel has 5 |
| 280 | Side channel has 6 |
| 281 | Consuming 7 |
| 282 | Side channel has 8 |
| 283 | Side channel has 9 |
| 284 | Consuming 10 |
| 285 | Done consuming |
| 286 | ``` |
| 287 | |
| 288 | <!--- TEST --> |
| 289 | |
| 290 | ### Selecting deferred values |
| 291 | |
| 292 | Deferred values can be selected using [onAwait][Deferred.onAwait] clause. |
| 293 | Let us start with an async function that returns a deferred string value after |
| 294 | a random delay: |
| 295 | |
| 296 | <!--- INCLUDE .*/example-select-04.kt |
| 297 | import java.util.* |
| 298 | --> |
| 299 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 300 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 301 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 302 | ```kotlin |
| 303 | fun CoroutineScope.asyncString(time: Int) = async { |
| 304 | delay(time.toLong()) |
| 305 | "Waited for $time ms" |
| 306 | } |
| 307 | ``` |
| 308 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 309 | </div> |
| 310 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 311 | Let us start a dozen of them with a random delay. |
| 312 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 313 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 314 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 315 | ```kotlin |
| 316 | fun CoroutineScope.asyncStringsList(): List<Deferred<String>> { |
| 317 | val random = Random(3) |
| 318 | return List(12) { asyncString(random.nextInt(1000)) } |
| 319 | } |
| 320 | ``` |
| 321 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 322 | </div> |
| 323 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 324 | Now the main function awaits for the first of them to complete and counts the number of deferred values |
| 325 | that are still active. Note, that we've used here the fact that `select` expression is a Kotlin DSL, |
| 326 | so we can provide clauses for it using an arbitrary code. In this case we iterate over a list |
| 327 | of deferred values to provide `onAwait` clause for each deferred value. |
| 328 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 329 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 330 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 331 | ```kotlin |
| 332 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 333 | val list = asyncStringsList() |
| 334 | val result = select<String> { |
| 335 | list.withIndex().forEach { (index, deferred) -> |
| 336 | deferred.onAwait { answer -> |
| 337 | "Deferred $index produced answer '$answer'" |
| 338 | } |
| 339 | } |
| 340 | } |
| 341 | println(result) |
| 342 | val countActive = list.count { it.isActive } |
| 343 | println("$countActive coroutines are still active") |
| 344 | } |
| 345 | ``` |
| 346 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 347 | </div> |
| 348 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 349 | > You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-select-04.kt) |
| 350 | |
| 351 | The output is: |
| 352 | |
| 353 | ```text |
| 354 | Deferred 4 produced answer 'Waited for 128 ms' |
| 355 | 11 coroutines are still active |
| 356 | ``` |
| 357 | |
| 358 | <!--- TEST --> |
| 359 | |
| 360 | ### Switch over a channel of deferred values |
| 361 | |
| 362 | Let us write a channel producer function that consumes a channel of deferred string values, waits for each received |
| 363 | deferred value, but only until the next deferred value comes over or the channel is closed. This example puts together |
| 364 | [onReceiveOrNull][ReceiveChannel.onReceiveOrNull] and [onAwait][Deferred.onAwait] clauses in the same `select`: |
| 365 | |
| 366 | <!--- INCLUDE |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame^] | 367 | import kotlin.coroutines.* |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 368 | --> |
| 369 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 370 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 371 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 372 | ```kotlin |
| 373 | fun CoroutineScope.switchMapDeferreds(input: ReceiveChannel<Deferred<String>>) = produce<String> { |
| 374 | var current = input.receive() // start with first received deferred value |
| 375 | while (isActive) { // loop while not cancelled/closed |
| 376 | val next = select<Deferred<String>?> { // return next deferred value from this select or null |
| 377 | input.onReceiveOrNull { update -> |
| 378 | update // replaces next value to wait |
| 379 | } |
| 380 | current.onAwait { value -> |
| 381 | send(value) // send value that current deferred has produced |
| 382 | input.receiveOrNull() // and use the next deferred from the input channel |
| 383 | } |
| 384 | } |
| 385 | if (next == null) { |
| 386 | println("Channel was closed") |
| 387 | break // out of loop |
| 388 | } else { |
| 389 | current = next |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | ``` |
| 394 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 395 | </div> |
| 396 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 397 | To test it, we'll use a simple async function that resolves to a specified string after a specified time: |
| 398 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 399 | |
| 400 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 401 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 402 | ```kotlin |
| 403 | fun CoroutineScope.asyncString(str: String, time: Long) = async { |
| 404 | delay(time) |
| 405 | str |
| 406 | } |
| 407 | ``` |
| 408 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 409 | </div> |
| 410 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 411 | The main function just launches a coroutine to print results of `switchMapDeferreds` and sends some test |
| 412 | data to it: |
| 413 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 414 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 415 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 416 | ```kotlin |
| 417 | fun main(args: Array<String>) = runBlocking<Unit> { |
| 418 | val chan = Channel<Deferred<String>>() // the channel for test |
| 419 | launch { // launch printing coroutine |
| 420 | for (s in switchMapDeferreds(chan)) |
| 421 | println(s) // print each received string |
| 422 | } |
| 423 | chan.send(asyncString("BEGIN", 100)) |
| 424 | delay(200) // enough time for "BEGIN" to be produced |
| 425 | chan.send(asyncString("Slow", 500)) |
| 426 | delay(100) // not enough time to produce slow |
| 427 | chan.send(asyncString("Replace", 100)) |
| 428 | delay(500) // give it time before the last one |
| 429 | chan.send(asyncString("END", 500)) |
| 430 | delay(1000) // give it time to process |
| 431 | chan.close() // close the channel ... |
| 432 | delay(500) // and wait some time to let it finish |
| 433 | } |
| 434 | ``` |
| 435 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 436 | </div> |
| 437 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 438 | > You can get full code [here](../core/kotlinx-coroutines-core/test/guide/example-select-05.kt) |
| 439 | |
| 440 | The result of this code: |
| 441 | |
| 442 | ```text |
| 443 | BEGIN |
| 444 | Replace |
| 445 | END |
| 446 | Channel was closed |
| 447 | ``` |
| 448 | |
| 449 | <!--- TEST --> |
| 450 | |
| 451 | <!--- MODULE kotlinx-coroutines-core --> |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame^] | 452 | <!--- INDEX kotlinx.coroutines --> |
| 453 | [Deferred.onAwait]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-deferred/on-await.html |
| 454 | <!--- INDEX kotlinx.coroutines.channels --> |
| 455 | [ReceiveChannel.receive]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-receive-channel/receive.html |
| 456 | [ReceiveChannel.onReceive]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-receive-channel/on-receive.html |
| 457 | [ReceiveChannel.onReceiveOrNull]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-receive-channel/on-receive-or-null.html |
| 458 | [SendChannel.send]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-send-channel/send.html |
| 459 | [SendChannel.onSend]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-send-channel/on-send.html |
| 460 | <!--- INDEX kotlinx.coroutines.selects --> |
| 461 | [select]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.selects/select.html |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 462 | <!--- END --> |