diff --git a/docs/topics/cancellation-and-timeouts.md b/docs/topics/cancellation-and-timeouts.md index c6a1b921b8..c574e12624 100644 --- a/docs/topics/cancellation-and-timeouts.md +++ b/docs/topics/cancellation-and-timeouts.md @@ -424,7 +424,7 @@ fun main() { If you run the above code, you'll see that it does not always print zero, though it may depend on the timings of your machine. You may need to tweak the timeout in this example to actually see non-zero values. -> Note that incrementing and decrementing `acquired` counter here from 100K coroutines is completely thread-safe, +> Note that incrementing and decrementing `acquired` counter here from 10K coroutines is completely thread-safe, > since it always happens from the same thread, the one used by `runBlocking`. > More on that will be explained in the chapter on coroutine context. > diff --git a/docs/topics/coroutines-basics.md b/docs/topics/coroutines-basics.md index af69894236..b4a7c0a7ca 100644 --- a/docs/topics/coroutines-basics.md +++ b/docs/topics/coroutines-basics.md @@ -250,14 +250,14 @@ Done Coroutines are less resource-intensive than JVM threads. Code that exhausts the JVM's available memory when using threads can be expressed using coroutines without hitting resource limits. For example, the following code launches -100000 distinct coroutines that each wait 5 seconds and then print a period +50,000 distinct coroutines that each waits 5 seconds and then prints a period ('.') while consuming very little memory: ```kotlin import kotlinx.coroutines.* fun main() = runBlocking { - repeat(100_000) { // launch a lot of coroutines + repeat(50_000) { // launch a lot of coroutines launch { delay(5000L) print(".") @@ -265,18 +265,19 @@ fun main() = runBlocking { } } ``` - +{kotlin-runnable="true" kotlin-min-compiler-version="1.3"} > You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt). > {type="note"} - + If you write the same program using threads (remove `runBlocking`, replace `launch` with `thread`, and replace `delay` with `Thread.sleep`), it will -likely consume too much memory and throw an out-of-memory error. +consume a lot of memory. Depending on your operating system, JDK version, +and its settings, it will either throw an out-of-memory error or start threads slowly +so that there are never too many concurrently running threads. diff --git a/kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt b/kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt index 24b890a0ad..3c503ac1cb 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt @@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleBasic06 import kotlinx.coroutines.* fun main() = runBlocking { - repeat(100_000) { // launch a lot of coroutines + repeat(50_000) { // launch a lot of coroutines launch { delay(5000L) print(".") diff --git a/kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt b/kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt index 7e54fb1d26..550f8e7b24 100644 --- a/kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt +++ b/kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt @@ -55,7 +55,7 @@ class BasicsGuideTest { @Test fun testExampleBasic06() { test("ExampleBasic06") { kotlinx.coroutines.guide.exampleBasic06.main() }.also { lines -> - check(lines.size == 1 && lines[0] == ".".repeat(100_000)) + check(lines.size == 1 && lines[0] == ".".repeat(50_000)) } } }