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 | --> |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 9 | <!--- KNIT ../kotlinx-coroutines-core/jvm/test/guide/.*\.kt --> |
| 10 | <!--- TEST_OUT ../kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 11 | // 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] | 12 | package kotlinx.coroutines.guide.test |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 13 | |
| 14 | import org.junit.Test |
| 15 | |
| 16 | class BasicsGuideTest { |
| 17 | --> |
| 18 | |
Prendota | b8a559d | 2018-11-30 16:24:23 +0300 | [diff] [blame] | 19 | **Table of contents** |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 20 | |
| 21 | <!--- TOC --> |
| 22 | |
| 23 | * [Coroutine basics](#coroutine-basics) |
| 24 | * [Your first coroutine](#your-first-coroutine) |
| 25 | * [Bridging blocking and non-blocking worlds](#bridging-blocking-and-non-blocking-worlds) |
| 26 | * [Waiting for a job](#waiting-for-a-job) |
| 27 | * [Structured concurrency](#structured-concurrency) |
| 28 | * [Scope builder](#scope-builder) |
| 29 | * [Extract function refactoring](#extract-function-refactoring) |
| 30 | * [Coroutines ARE light-weight](#coroutines-are-light-weight) |
| 31 | * [Global coroutines are like daemon threads](#global-coroutines-are-like-daemon-threads) |
| 32 | |
| 33 | <!--- END_TOC --> |
| 34 | |
| 35 | |
| 36 | ## Coroutine basics |
| 37 | |
| 38 | This section covers basic coroutine concepts. |
| 39 | |
| 40 | ### Your first coroutine |
| 41 | |
| 42 | Run the following code: |
| 43 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 44 | <div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3"> |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 45 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 46 | ```kotlin |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 47 | import kotlinx.coroutines.* |
| 48 | |
| 49 | fun main() { |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 50 | GlobalScope.launch { // launch new coroutine in background and continue |
| 51 | delay(1000L) // non-blocking delay for 1 second (default time unit is ms) |
| 52 | println("World!") // print after delay |
| 53 | } |
| 54 | println("Hello,") // main thread continues while coroutine is delayed |
| 55 | Thread.sleep(2000L) // block main thread for 2 seconds to keep JVM alive |
| 56 | } |
| 57 | ``` |
| 58 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 59 | </div> |
| 60 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 61 | > You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-01.kt) |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 62 | |
Marcin MoskaĆa | e5bf9ae | 2018-10-08 09:59:41 +0300 | [diff] [blame] | 63 | You will see the following result: |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 64 | |
| 65 | ```text |
| 66 | Hello, |
| 67 | World! |
| 68 | ``` |
| 69 | |
| 70 | <!--- TEST --> |
| 71 | |
| 72 | Essentially, coroutines are light-weight threads. |
| 73 | They are launched with [launch] _coroutine builder_ in a context of some [CoroutineScope]. |
| 74 | Here we are launching a new coroutine in the [GlobalScope], meaning that the lifetime of the new |
| 75 | coroutine is limited only by the lifetime of the whole application. |
| 76 | |
| 77 | You can achieve the same result replacing |
| 78 | `GlobalScope.launch { ... }` with `thread { ... }` and `delay(...)` with `Thread.sleep(...)`. Try it. |
| 79 | |
| 80 | If you start by replacing `GlobalScope.launch` by `thread`, the compiler produces the following error: |
| 81 | |
| 82 | ``` |
| 83 | Error: Kotlin: Suspend functions are only allowed to be called from a coroutine or another suspend function |
| 84 | ``` |
| 85 | |
| 86 | That is because [delay] is a special _suspending function_ that does not block a thread, but _suspends_ |
| 87 | coroutine and it can be only used from a coroutine. |
| 88 | |
| 89 | ### Bridging blocking and non-blocking worlds |
| 90 | |
| 91 | The first example mixes _non-blocking_ `delay(...)` and _blocking_ `Thread.sleep(...)` in the same code. |
Devin B | b104cfc | 2018-11-12 11:51:00 -0500 | [diff] [blame] | 92 | It is easy to lose track of which one is blocking and which one is not. |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 93 | Let's be explicit about blocking using [runBlocking] coroutine builder: |
| 94 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 95 | <div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3"> |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 96 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 97 | ```kotlin |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 98 | import kotlinx.coroutines.* |
| 99 | |
| 100 | fun main() { |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 101 | GlobalScope.launch { // launch new coroutine in background and continue |
| 102 | delay(1000L) |
| 103 | println("World!") |
| 104 | } |
| 105 | println("Hello,") // main thread continues here immediately |
| 106 | runBlocking { // but this expression blocks the main thread |
| 107 | delay(2000L) // ... while we delay for 2 seconds to keep JVM alive |
| 108 | } |
| 109 | } |
| 110 | ``` |
| 111 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 112 | </div> |
| 113 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 114 | > You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-02.kt) |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 115 | |
| 116 | <!--- TEST |
| 117 | Hello, |
| 118 | World! |
| 119 | --> |
| 120 | |
| 121 | The result is the same, but this code uses only non-blocking [delay]. |
Inego | 596187e | 2019-04-21 14:21:53 +0700 | [diff] [blame^] | 122 | The main thread invoking `runBlocking` _blocks_ until the coroutine inside `runBlocking` completes. |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 123 | |
| 124 | This example can be also rewritten in a more idiomatic way, using `runBlocking` to wrap |
| 125 | the execution of the main function: |
| 126 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 127 | <div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3"> |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 128 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 129 | ```kotlin |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 130 | import kotlinx.coroutines.* |
| 131 | |
| 132 | fun main() = runBlocking<Unit> { // start main coroutine |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 133 | GlobalScope.launch { // launch new coroutine in background and continue |
| 134 | delay(1000L) |
| 135 | println("World!") |
| 136 | } |
| 137 | println("Hello,") // main coroutine continues here immediately |
| 138 | delay(2000L) // delaying for 2 seconds to keep JVM alive |
| 139 | } |
| 140 | ``` |
| 141 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 142 | </div> |
| 143 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 144 | > You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-02b.kt) |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 145 | |
| 146 | <!--- TEST |
| 147 | Hello, |
| 148 | World! |
| 149 | --> |
| 150 | |
| 151 | Here `runBlocking<Unit> { ... }` works as an adaptor that is used to start the top-level main coroutine. |
| 152 | We explicitly specify its `Unit` return type, because a well-formed `main` function in Kotlin has to return `Unit`. |
| 153 | |
Inego | 596187e | 2019-04-21 14:21:53 +0700 | [diff] [blame^] | 154 | This is also a way to write unit tests for suspending functions: |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 155 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 156 | <!--- INCLUDE |
| 157 | import kotlinx.coroutines.* |
| 158 | --> |
| 159 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 160 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 161 | |
| 162 | ```kotlin |
| 163 | class MyTest { |
| 164 | @Test |
| 165 | fun testMySuspendingFunction() = runBlocking<Unit> { |
| 166 | // here we can use suspending functions using any assertion style that we like |
| 167 | } |
| 168 | } |
| 169 | ``` |
| 170 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 171 | </div> |
| 172 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 173 | <!--- CLEAR --> |
| 174 | |
| 175 | ### Waiting for a job |
| 176 | |
| 177 | Delaying for a time while another coroutine is working is not a good approach. Let's explicitly |
| 178 | wait (in a non-blocking way) until the background [Job] that we have launched is complete: |
| 179 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 180 | <div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3"> |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 181 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 182 | ```kotlin |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 183 | import kotlinx.coroutines.* |
| 184 | |
| 185 | fun main() = runBlocking { |
| 186 | //sampleStart |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 187 | val job = GlobalScope.launch { // launch new coroutine and keep a reference to its Job |
| 188 | delay(1000L) |
| 189 | println("World!") |
| 190 | } |
| 191 | println("Hello,") |
| 192 | job.join() // wait until child coroutine completes |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 193 | //sampleEnd |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 194 | } |
| 195 | ``` |
| 196 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 197 | </div> |
| 198 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 199 | > You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-03.kt) |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 200 | |
| 201 | <!--- TEST |
| 202 | Hello, |
| 203 | World! |
| 204 | --> |
| 205 | |
| 206 | Now the result is still the same, but the code of the main coroutine is not tied to the duration of |
| 207 | the background job in any way. Much better. |
| 208 | |
| 209 | ### Structured concurrency |
| 210 | |
| 211 | There is still something to be desired for practical usage of coroutines. |
Inego | 596187e | 2019-04-21 14:21:53 +0700 | [diff] [blame^] | 212 | When we use `GlobalScope.launch`, we create a top-level coroutine. Even though it is light-weight, it still |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 213 | consumes some memory resources while it runs. If we forget to keep a reference to the newly launched |
| 214 | coroutine it still runs. What if the code in the coroutine hangs (for example, we erroneously |
| 215 | delay for too long), what if we launched too many coroutines and ran out of memory? |
Inego | 596187e | 2019-04-21 14:21:53 +0700 | [diff] [blame^] | 216 | Having to manually keep references to all the launched coroutines and [join][Job.join] them is error-prone. |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 217 | |
| 218 | There is a better solution. We can use structured concurrency in our code. |
| 219 | Instead of launching coroutines in the [GlobalScope], just like we usually do with threads (threads are always global), |
| 220 | we can launch coroutines in the specific scope of the operation we are performing. |
| 221 | |
| 222 | In our example, we have `main` function that is turned into a coroutine using [runBlocking] coroutine builder. |
saied89 | a223f35 | 2018-10-30 22:44:20 +0330 | [diff] [blame] | 223 | Every coroutine builder, including `runBlocking`, adds an instance of [CoroutineScope] to the scope of its code block. |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 224 | We can launch coroutines in this scope without having to `join` them explicitly, because |
| 225 | an outer coroutine (`runBlocking` in our example) does not complete until all the coroutines launched |
| 226 | in its scope complete. Thus, we can make our example simpler: |
| 227 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 228 | <div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3"> |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 229 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 230 | ```kotlin |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 231 | import kotlinx.coroutines.* |
| 232 | |
| 233 | fun main() = runBlocking { // this: CoroutineScope |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 234 | launch { // launch new coroutine in the scope of runBlocking |
| 235 | delay(1000L) |
| 236 | println("World!") |
| 237 | } |
| 238 | println("Hello,") |
| 239 | } |
| 240 | ``` |
| 241 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 242 | </div> |
| 243 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 244 | > You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-03s.kt) |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 245 | |
| 246 | <!--- TEST |
| 247 | Hello, |
| 248 | World! |
| 249 | --> |
| 250 | |
| 251 | ### Scope builder |
| 252 | In addition to the coroutine scope provided by different builders, it is possible to declare your own scope using |
| 253 | [coroutineScope] builder. It creates new coroutine scope and does not complete until all launched children |
| 254 | complete. The main difference between [runBlocking] and [coroutineScope] is that the latter does not block the current thread |
| 255 | while waiting for all children to complete. |
| 256 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 257 | <div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3"> |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 258 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 259 | ```kotlin |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 260 | import kotlinx.coroutines.* |
| 261 | |
| 262 | fun main() = runBlocking { // this: CoroutineScope |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 263 | launch { |
| 264 | delay(200L) |
| 265 | println("Task from runBlocking") |
| 266 | } |
| 267 | |
| 268 | coroutineScope { // Creates a new coroutine scope |
| 269 | launch { |
| 270 | delay(500L) |
| 271 | println("Task from nested launch") |
| 272 | } |
| 273 | |
| 274 | delay(100L) |
| 275 | println("Task from coroutine scope") // This line will be printed before nested launch |
| 276 | } |
| 277 | |
| 278 | println("Coroutine scope is over") // This line is not printed until nested launch completes |
| 279 | } |
| 280 | ``` |
| 281 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 282 | </div> |
| 283 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 284 | > You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-04.kt) |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 285 | |
| 286 | <!--- TEST |
| 287 | Task from coroutine scope |
| 288 | Task from runBlocking |
| 289 | Task from nested launch |
| 290 | Coroutine scope is over |
| 291 | --> |
| 292 | |
| 293 | ### Extract function refactoring |
| 294 | |
| 295 | Let's extract the block of code inside `launch { ... }` into a separate function. When you |
| 296 | perform "Extract function" refactoring on this code you get a new function with `suspend` modifier. |
| 297 | That is your first _suspending function_. Suspending functions can be used inside coroutines |
| 298 | just like regular functions, but their additional feature is that they can, in turn, |
| 299 | use other suspending functions, like `delay` in this example, to _suspend_ execution of a coroutine. |
| 300 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 301 | <div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3"> |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 302 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 303 | ```kotlin |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 304 | import kotlinx.coroutines.* |
| 305 | |
| 306 | fun main() = runBlocking { |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 307 | launch { doWorld() } |
| 308 | println("Hello,") |
| 309 | } |
| 310 | |
| 311 | // this is your first suspending function |
| 312 | suspend fun doWorld() { |
| 313 | delay(1000L) |
| 314 | println("World!") |
| 315 | } |
| 316 | ``` |
| 317 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 318 | </div> |
| 319 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 320 | > You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-05.kt) |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 321 | |
| 322 | <!--- TEST |
| 323 | Hello, |
| 324 | World! |
| 325 | --> |
| 326 | |
| 327 | |
| 328 | But what if the extracted function contains a coroutine builder which is invoked on the current scope? |
| 329 | In this case `suspend` modifier on the extracted function is not enough. Making `doWorld` extension |
| 330 | method on `CoroutineScope` is one of the solutions, but it may not always be applicable as it does not make API clearer. |
Vsevolod Tolstopyatov | fac516f | 2018-09-28 14:38:29 +0300 | [diff] [blame] | 331 | Idiomatic solution is to have either explicit `CoroutineScope` as a field in a class containing target function |
| 332 | or implicit when outer class implements `CoroutineScope`. |
| 333 | As a last resort, [CoroutineScope(coroutineContext)][CoroutineScope()] can be used, but such approach is structurally unsafe |
| 334 | because you no longer have control on the scope this method is executed. Only private API can use this builder. |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 335 | |
| 336 | ### Coroutines ARE light-weight |
| 337 | |
| 338 | Run the following code: |
| 339 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 340 | <div class="sample" markdown="1" theme="idea" data-highlight-only> |
| 341 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 342 | ```kotlin |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 343 | import kotlinx.coroutines.* |
| 344 | |
| 345 | fun main() = runBlocking { |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 346 | repeat(100_000) { // launch a lot of coroutines |
| 347 | launch { |
| 348 | delay(1000L) |
| 349 | print(".") |
| 350 | } |
| 351 | } |
| 352 | } |
| 353 | ``` |
| 354 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 355 | </div> |
| 356 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 357 | > You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt) |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 358 | |
| 359 | <!--- TEST lines.size == 1 && lines[0] == ".".repeat(100_000) --> |
| 360 | |
| 361 | It launches 100K coroutines and, after a second, each coroutine prints a dot. |
| 362 | Now, try that with threads. What would happen? (Most likely your code will produce some sort of out-of-memory error) |
| 363 | |
| 364 | ### Global coroutines are like daemon threads |
| 365 | |
| 366 | The following code launches a long-running coroutine in [GlobalScope] that prints "I'm sleeping" twice a second and then |
| 367 | returns from the main function after some delay: |
| 368 | |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 369 | <div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3"> |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 370 | |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 371 | ```kotlin |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 372 | import kotlinx.coroutines.* |
| 373 | |
| 374 | fun main() = runBlocking { |
| 375 | //sampleStart |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 376 | GlobalScope.launch { |
| 377 | repeat(1000) { i -> |
| 378 | println("I'm sleeping $i ...") |
| 379 | delay(500L) |
| 380 | } |
| 381 | } |
| 382 | delay(1300L) // just quit after delay |
Prendota | 65e6c8c | 2018-10-17 11:51:08 +0300 | [diff] [blame] | 383 | //sampleEnd |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 384 | } |
| 385 | ``` |
| 386 | |
Alexander Prendota | cbeef10 | 2018-09-27 18:42:04 +0300 | [diff] [blame] | 387 | </div> |
| 388 | |
Vsevolod Tolstopyatov | e50a0fa | 2019-01-28 11:34:24 +0300 | [diff] [blame] | 389 | > You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-07.kt) |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 390 | |
| 391 | You can run and see that it prints three lines and terminates: |
| 392 | |
| 393 | ```text |
| 394 | I'm sleeping 0 ... |
| 395 | I'm sleeping 1 ... |
| 396 | I'm sleeping 2 ... |
| 397 | ``` |
| 398 | |
| 399 | <!--- TEST --> |
| 400 | |
| 401 | Active coroutines that were launched in [GlobalScope] do not keep the process alive. They are like daemon threads. |
| 402 | |
Roman Elizarov | 99c28aa | 2018-09-23 18:42:36 +0300 | [diff] [blame] | 403 | <!--- MODULE kotlinx-coroutines-core --> |
Roman Elizarov | 0950dfa | 2018-07-13 10:33:25 +0300 | [diff] [blame] | 404 | <!--- INDEX kotlinx.coroutines --> |
| 405 | [launch]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html |
| 406 | [CoroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html |
| 407 | [GlobalScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-global-scope/index.html |
| 408 | [delay]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/delay.html |
| 409 | [runBlocking]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/run-blocking.html |
| 410 | [Job]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html |
| 411 | [Job.join]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/join.html |
| 412 | [coroutineScope]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html |
| 413 | [CoroutineScope()]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope.html |
Roman Elizarov | 99c28aa | 2018-09-23 18:42:36 +0300 | [diff] [blame] | 414 | <!--- END --> |
hadihariri | 7db5553 | 2018-09-15 10:35:08 +0200 | [diff] [blame] | 415 | |
| 416 | |