Skip to content

Commit b0c510c

Browse files
committed
Merge branch 'master' into develop
# Conflicts: # README.md
2 parents 915347c + 2653cd2 commit b0c510c

File tree

6 files changed

+41
-37
lines changed

6 files changed

+41
-37
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
[![Kotlin Stable](https://kotl.in/badges/stable.svg)](https://kotlinlang.org/docs/components-stability.html)
44
[![JetBrains official project](https://jb.gg/badges/official.svg)](https://confluence.jetbrains.com/display/ALL/JetBrains+on+GitHub)
55
[![GitHub license](https://img.shields.io/badge/license-Apache%20License%202.0-blue.svg?style=flat)](https://www.apache.org/licenses/LICENSE-2.0)
6-
[![Download](https://img.shields.io/maven-central/v/org.jetbrains.kotlinx/kotlinx-coroutines-core/1.7.0-Beta)](https://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core/1.7.0-Beta/pom)
6+
[![Download](https://img.shields.io/maven-central/v/org.jetbrains.kotlinx/kotlinx-coroutines-core/1.7.0-Beta)](https://central.sonatype.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core/1.7.0-Beta)
77
[![Kotlin](https://img.shields.io/badge/kotlin-1.8.20-blue.svg?logo=kotlin)](http://kotlinlang.org)
88
[![Slack channel](https://img.shields.io/badge/chat-slack-green.svg?logo=slack)](https://kotlinlang.slack.com/messages/coroutines/)
99

@@ -180,13 +180,13 @@ Platform-specific dependencies are recommended to be used only for non-multiplat
180180
#### JS
181181

182182
Kotlin/JS version of `kotlinx.coroutines` is published as
183-
[`kotlinx-coroutines-core-js`](https://search.maven.org/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core-js/1.7.0-Beta/jar)
183+
[`kotlinx-coroutines-core-js`](https://central.sonatype.com/artifact/org.jetbrains.kotlinx/kotlinx-coroutines-core-js/1.7.0-Beta)
184184
(follow the link to get the dependency declaration snippet) and as [`kotlinx-coroutines-core`](https://www.npmjs.com/package/kotlinx-coroutines-core) NPM package.
185185

186186
#### Native
187187

188188
Kotlin/Native version of `kotlinx.coroutines` is published as
189-
[`kotlinx-coroutines-core-$platform`](https://mvnrepository.com/search?q=kotlinx-coroutines-core-) where `$platform` is
189+
[`kotlinx-coroutines-core-$platform`](https://central.sonatype.com/search?q=kotlinx-coroutines-core&namespace=org.jetbrains.kotlinx) where `$platform` is
190190
the target Kotlin/Native platform.
191191
Targets are provided in accordance with [official K/N target support](https://kotlinlang.org/docs/native-target-support.html).
192192
## Building and Contributing

docs/topics/cancellation-and-timeouts.md

Lines changed: 1 addition & 1 deletion
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-and-channels.md

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -870,13 +870,33 @@ completion or cancel it explicitly, but that won't happen automatically as it wo
870870
871871
### Canceling the loading of contributors
872872
873-
Consider two versions of the `loadContributorsConcurrent()` function. The first uses `coroutineScope` to start all of the
874-
child coroutines, whereas the second uses `GlobalScope`. Compare how both versions behave when you try to cancel
875-
the parent coroutine.
873+
Create two versions of the function that loads the list of contributors. Compare how both versions behave when you try to
874+
cancel the parent coroutine. The first version will use `coroutineScope` to start all of the child coroutines,
875+
whereas the second will use `GlobalScope`.
876+
877+
1. In `Request5Concurrent.kt`, add a 3-second delay to the `loadContributorsConcurrent()` function:
878+
879+
```kotlin
880+
suspend fun loadContributorsConcurrent(
881+
service: GitHubService,
882+
req: RequestData
883+
): List<User> = coroutineScope {
884+
// ...
885+
async {
886+
log("starting loading for ${repo.name}")
887+
delay(3000)
888+
// load repo contributors
889+
}
890+
// ...
891+
}
892+
```
893+
894+
The delay affects all of the coroutines that send requests, so that there's enough time to cancel the loading
895+
after the coroutines are started but before the requests are sent.
876896

877-
1. Copy the implementation of `loadContributorsConcurrent()` from `Request5Concurrent.kt` to
878-
`loadContributorsNotCancellable()` in `Request5NotCancellable.kt`, and then remove the creation of a new `coroutineScope`.
879-
2. The `async` calls now fail to resolve, so start them by using `GlobalScope.async`:
897+
2. Create the second version of the loading function: copy the implementation of `loadContributorsConcurrent()` to
898+
`loadContributorsNotCancellable()` in `Request5NotCancellable.kt` and then remove the creation of a new `coroutineScope`.
899+
3. The `async` calls now fail to resolve, so start them by using `GlobalScope.async`:
880900

881901
```kotlin
882902
suspend fun loadContributorsNotCancellable(
@@ -894,25 +914,8 @@ the parent coroutine.
894914
```
895915

896916
* The function now returns the result directly, not as the last expression inside the lambda (lines `#1` and `#3`).
897-
* All of the "contributors" coroutines are started inside the `GlobalScope`, not as children of the coroutine scope (
898-
line `#2`).
899-
3. Add a 3-second delay to all of the coroutines that send requests, so that there's enough time to cancel the loading
900-
after the coroutines are started but before the requests are sent:
901-
902-
```kotlin
903-
suspend fun loadContributorsConcurrent(
904-
service: GitHubService,
905-
req: RequestData
906-
): List<User> = coroutineScope {
907-
// ...
908-
async {
909-
log("starting loading for ${repo.name}")
910-
delay(3000)
911-
// load repo contributors
912-
}
913-
// ...
914-
}
915-
```
917+
* All of the "contributors" coroutines are started inside the `GlobalScope`, not as children of the coroutine scope
918+
(line `#2`).
916919

917920
4. Run the program and choose the _CONCURRENT_ option to load the contributors.
918921
5. Wait until all of the "contributors" coroutines are started, and then click _Cancel_. The log shows no new results,

docs/topics/coroutines-basics.md

Lines changed: 7 additions & 6 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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)