Skip to content

Commit 2653cd2

Browse files
KT-51515 cleanup (#3649)
* 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 <[email protected]>
1 parent 3fc1c20 commit 2653cd2

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

docs/topics/cancellation-and-timeouts.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ fun main() {
424424
If you run the above code, you'll see that it does not always print zero, though it may depend on the timings
425425
of your machine. You may need to tweak the timeout in this example to actually see non-zero values.
426426

427-
> Note that incrementing and decrementing `acquired` counter here from 100K coroutines is completely thread-safe,
427+
> Note that incrementing and decrementing `acquired` counter here from 10K coroutines is completely thread-safe,
428428
> since it always happens from the same thread, the one used by `runBlocking`.
429429
> More on that will be explained in the chapter on coroutine context.
430430
>

docs/topics/coroutines-basics.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -250,33 +250,34 @@ Done
250250
Coroutines are less resource-intensive than JVM threads. Code that exhausts the
251251
JVM's available memory when using threads can be expressed using coroutines
252252
without hitting resource limits. For example, the following code launches
253-
100000 distinct coroutines that each wait 5 seconds and then print a period
253+
50,000 distinct coroutines that each waits 5 seconds and then prints a period
254254
('.') while consuming very little memory:
255255

256256
```kotlin
257257
import kotlinx.coroutines.*
258258

259259
fun main() = runBlocking {
260-
repeat(100_000) { // launch a lot of coroutines
260+
repeat(50_000) { // launch a lot of coroutines
261261
launch {
262262
delay(5000L)
263263
print(".")
264264
}
265265
}
266266
}
267267
```
268-
<!-- While coroutines do have a smaller memory footprint than threads, this
269-
example will exhaust the playground's heap memory; don't make it runnable. -->
268+
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
270269

271270
> You can get the full code [here](../../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt).
272271
>
273272
{type="note"}
274273

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

277276
If you write the same program using threads (remove `runBlocking`, replace
278277
`launch` with `thread`, and replace `delay` with `Thread.sleep`), it will
279-
likely consume too much memory and throw an out-of-memory error.
278+
consume a lot of memory. Depending on your operating system, JDK version,
279+
and its settings, it will either throw an out-of-memory error or start threads slowly
280+
so that there are never too many concurrently running threads.
280281

281282
<!--- MODULE kotlinx-coroutines-core -->
282283
<!--- INDEX kotlinx.coroutines -->

kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package kotlinx.coroutines.guide.exampleBasic06
88
import kotlinx.coroutines.*
99

1010
fun main() = runBlocking {
11-
repeat(100_000) { // launch a lot of coroutines
11+
repeat(50_000) { // launch a lot of coroutines
1212
launch {
1313
delay(5000L)
1414
print(".")

kotlinx-coroutines-core/jvm/test/guide/test/BasicsGuideTest.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class BasicsGuideTest {
5555
@Test
5656
fun testExampleBasic06() {
5757
test("ExampleBasic06") { kotlinx.coroutines.guide.exampleBasic06.main() }.also { lines ->
58-
check(lines.size == 1 && lines[0] == ".".repeat(100_000))
58+
check(lines.size == 1 && lines[0] == ".".repeat(50_000))
5959
}
6060
}
6161
}

0 commit comments

Comments
 (0)