Skip to content

Commit

Permalink
KT-51515 cleanup (#3649)
Browse files Browse the repository at this point in the history
* Fix comment about 100k coroutines in cancellation and timeouts section

Changed to 10K consistently with the code.

* Make "Coroutines are light-weight" example runnable

Reduce the number of coroutines to 50K - works on playground.
Also, change the working about what happens with threads.

Co-authored-by: Danil Pavlov <danil.pavlov@jetbrains.com>
  • Loading branch information
elizarov and danil-pavlov committed Apr 17, 2023
1 parent 3fc1c20 commit 2653cd2
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 9 deletions.
2 changes: 1 addition & 1 deletion docs/topics/cancellation-and-timeouts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
>
Expand Down
13 changes: 7 additions & 6 deletions docs/topics/coroutines-basics.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,33 +250,34 @@ 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(".")
}
}
}
```
<!-- While coroutines do have a smaller memory footprint than threads, this
example will exhaust the playground's heap memory; don't make it runnable. -->
{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"}

<!--- TEST lines.size == 1 && lines[0] == ".".repeat(100_000) -->
<!--- TEST lines.size == 1 && lines[0] == ".".repeat(50_000) -->

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.

<!--- MODULE kotlinx-coroutines-core -->
<!--- INDEX kotlinx.coroutines -->
Expand Down
2 changes: 1 addition & 1 deletion kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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(".")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}

0 comments on commit 2653cd2

Please sign in to comment.