Skip to content

KT-51515 cleanup #3649

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
}