Skip to content

Commit 634b907

Browse files
committed
Merge remote-tracking branch 'origin/master' into develop
2 parents 755d76b + de61a74 commit 634b907

12 files changed

+99
-95
lines changed

Diff for: docs/basics.md

+31-30
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fun main() {
4141

4242
</div>
4343

44-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-01.kt).
44+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-01.kt).
4545
4646
You will see the following result:
4747

@@ -57,24 +57,24 @@ They are launched with [launch] _coroutine builder_ in a context of some [Corout
5757
Here we are launching a new coroutine in the [GlobalScope], meaning that the lifetime of the new
5858
coroutine is limited only by the lifetime of the whole application.
5959

60-
You can achieve the same result replacing
61-
`GlobalScope.launch { ... }` with `thread { ... }` and `delay(...)` with `Thread.sleep(...)`.
60+
You can achieve the same result by replacing
61+
`GlobalScope.launch { ... }` with `thread { ... }`, and `delay(...)` with `Thread.sleep(...)`.
6262
Try it (don't forget to import `kotlin.concurrent.thread`).
6363

64-
If you start by replacing `GlobalScope.launch` by `thread`, the compiler produces the following error:
64+
If you start by replacing `GlobalScope.launch` with `thread`, the compiler produces the following error:
6565

6666
```
6767
Error: Kotlin: Suspend functions are only allowed to be called from a coroutine or another suspend function
6868
```
6969

70-
That is because [delay] is a special _suspending function_ that does not block a thread, but _suspends_
71-
coroutine and it can be only used from a coroutine.
70+
That is because [delay] is a special _suspending function_ that does not block a thread, but _suspends_ the
71+
coroutine, and it can be only used from a coroutine.
7272

7373
### Bridging blocking and non-blocking worlds
7474

7575
The first example mixes _non-blocking_ `delay(...)` and _blocking_ `Thread.sleep(...)` in the same code.
7676
It is easy to lose track of which one is blocking and which one is not.
77-
Let's be explicit about blocking using [runBlocking] coroutine builder:
77+
Let's be explicit about blocking using the [runBlocking] coroutine builder:
7878

7979
<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
8080

@@ -95,7 +95,7 @@ fun main() {
9595

9696
</div>
9797

98-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-02.kt).
98+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-02.kt).
9999
100100
<!--- TEST
101101
Hello,
@@ -125,7 +125,7 @@ fun main() = runBlocking<Unit> { // start main coroutine
125125

126126
</div>
127127

128-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-03.kt).
128+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-03.kt).
129129
130130
<!--- TEST
131131
Hello,
@@ -180,7 +180,7 @@ fun main() = runBlocking {
180180

181181
</div>
182182

183-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-04.kt).
183+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-04.kt).
184184
185185
<!--- TEST
186186
Hello,
@@ -195,15 +195,15 @@ the background job in any way. Much better.
195195
There is still something to be desired for practical usage of coroutines.
196196
When we use `GlobalScope.launch`, we create a top-level coroutine. Even though it is light-weight, it still
197197
consumes some memory resources while it runs. If we forget to keep a reference to the newly launched
198-
coroutine it still runs. What if the code in the coroutine hangs (for example, we erroneously
198+
coroutine, it still runs. What if the code in the coroutine hangs (for example, we erroneously
199199
delay for too long), what if we launched too many coroutines and ran out of memory?
200200
Having to manually keep references to all the launched coroutines and [join][Job.join] them is error-prone.
201201

202202
There is a better solution. We can use structured concurrency in our code.
203203
Instead of launching coroutines in the [GlobalScope], just like we usually do with threads (threads are always global),
204204
we can launch coroutines in the specific scope of the operation we are performing.
205205

206-
In our example, we have `main` function that is turned into a coroutine using [runBlocking] coroutine builder.
206+
In our example, we have a `main` function that is turned into a coroutine using the [runBlocking] coroutine builder.
207207
Every coroutine builder, including `runBlocking`, adds an instance of [CoroutineScope] to the scope of its code block.
208208
We can launch coroutines in this scope without having to `join` them explicitly, because
209209
an outer coroutine (`runBlocking` in our example) does not complete until all the coroutines launched
@@ -225,20 +225,20 @@ fun main() = runBlocking { // this: CoroutineScope
225225

226226
</div>
227227

228-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-05.kt).
228+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-05.kt).
229229
230230
<!--- TEST
231231
Hello,
232232
World!
233233
-->
234234

235235
### Scope builder
236-
In addition to the coroutine scope provided by different builders, it is possible to declare your own scope using
237-
[coroutineScope] builder. It creates a coroutine scope and does not complete until all launched children
238-
complete.
239236

240-
[runBlocking] and [coroutineScope] may look similar because they both wait for its body and all its children to complete.
241-
The main difference between these two is that the [runBlocking] method _blocks_ the current thread for waiting,
237+
In addition to the coroutine scope provided by different builders, it is possible to declare your own scope using the
238+
[coroutineScope] builder. It creates a coroutine scope and does not complete until all launched children complete.
239+
240+
[runBlocking] and [coroutineScope] may look similar because they both wait for their body and all its children to complete.
241+
The main difference is that the [runBlocking] method _blocks_ the current thread for waiting,
242242
while [coroutineScope] just suspends, releasing the underlying thread for other usages.
243243
Because of that difference, [runBlocking] is a regular function and [coroutineScope] is a suspending function.
244244

@@ -271,7 +271,7 @@ fun main() = runBlocking { // this: CoroutineScope
271271

272272
</div>
273273

274-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt).
274+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt).
275275
276276
<!--- TEST
277277
Task from coroutine scope
@@ -280,16 +280,16 @@ Task from nested launch
280280
Coroutine scope is over
281281
-->
282282

283-
Note that right after "Task from coroutine scope" message, while waiting for nested launch,
284-
"Task from runBlocking" is executed and printed, though coroutineScope is not completed yet.
283+
Note that right after the "Task from coroutine scope" message (while waiting for nested launch)
284+
"Task from runBlocking" is executed and printed — even though the [coroutineScope] is not completed yet.
285285

286286
### Extract function refactoring
287287

288288
Let's extract the block of code inside `launch { ... }` into a separate function. When you
289-
perform "Extract function" refactoring on this code you get a new function with `suspend` modifier.
290-
That is your first _suspending function_. Suspending functions can be used inside coroutines
289+
perform "Extract function" refactoring on this code, you get a new function with the `suspend` modifier.
290+
This is your first _suspending function_. Suspending functions can be used inside coroutines
291291
just like regular functions, but their additional feature is that they can, in turn,
292-
use other suspending functions, like `delay` in this example, to _suspend_ execution of a coroutine.
292+
use other suspending functions (like `delay` in this example) to _suspend_ execution of a coroutine.
293293

294294
<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
295295

@@ -310,7 +310,7 @@ suspend fun doWorld() {
310310

311311
</div>
312312

313-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-07.kt).
313+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-07.kt).
314314
315315
<!--- TEST
316316
Hello,
@@ -319,11 +319,11 @@ World!
319319

320320

321321
But what if the extracted function contains a coroutine builder which is invoked on the current scope?
322-
In this case `suspend` modifier on the extracted function is not enough. Making `doWorld` an extension
323-
method on `CoroutineScope` is one of the solutions, but it may not always be applicable as it does not make API clearer.
322+
In this case, the `suspend` modifier on the extracted function is not enough. Making `doWorld` an extension
323+
method on `CoroutineScope` is one of the solutions, but it may not always be applicable as it does not make the API clearer.
324324
The idiomatic solution is to have either an explicit `CoroutineScope` as a field in a class containing the target function
325325
or an implicit one when the outer class implements `CoroutineScope`.
326-
As a last resort, [CoroutineScope(coroutineContext)][CoroutineScope()] can be used, but such approach is structurally unsafe
326+
As a last resort, [CoroutineScope(coroutineContext)][CoroutineScope()] can be used, but such an approach is structurally unsafe
327327
because you no longer have control on the scope of execution of this method. Only private APIs can use this builder.
328328

329329
### Coroutines ARE light-weight
@@ -347,11 +347,12 @@ fun main() = runBlocking {
347347

348348
</div>
349349

350-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-08.kt).
350+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-08.kt).
351351
352352
<!--- TEST lines.size == 1 && lines[0] == ".".repeat(100_000) -->
353353

354354
It launches 100K coroutines and, after a second, each coroutine prints a dot.
355+
355356
Now, try that with threads. What would happen? (Most likely your code will produce some sort of out-of-memory error)
356357

357358
### Global coroutines are like daemon threads
@@ -379,7 +380,7 @@ fun main() = runBlocking {
379380

380381
</div>
381382

382-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-09.kt).
383+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-basic-09.kt).
383384
384385
You can run and see that it prints three lines and terminates:
385386

Diff for: docs/cancellation-and-timeouts.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fun main() = runBlocking {
4949

5050
</div>
5151

52-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-01.kt).
52+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-01.kt).
5353
5454
It produces the following output:
5555

@@ -104,7 +104,7 @@ fun main() = runBlocking {
104104

105105
</div>
106106

107-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-02.kt).
107+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-02.kt).
108108
109109
Run it to see that it continues to print "I'm sleeping" even after cancellation
110110
until the job completes by itself after five iterations.
@@ -156,7 +156,7 @@ fun main() = runBlocking {
156156

157157
</div>
158158

159-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.kt).
159+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.kt).
160160
161161
As you can see, now this loop is cancelled. [isActive] is an extension property
162162
available inside the coroutine via the [CoroutineScope] object.
@@ -203,7 +203,7 @@ fun main() = runBlocking {
203203

204204
</div>
205205

206-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-04.kt).
206+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-04.kt).
207207
208208
Both [join][Job.join] and [cancelAndJoin] wait for all finalization actions to complete,
209209
so the example above produces the following output:
@@ -259,7 +259,7 @@ fun main() = runBlocking {
259259

260260
</div>
261261

262-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-05.kt).
262+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-05.kt).
263263
264264
<!--- TEST
265265
job: I'm sleeping 0 ...
@@ -298,7 +298,7 @@ fun main() = runBlocking {
298298

299299
</div>
300300

301-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-06.kt).
301+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-06.kt).
302302
303303
It produces the following output:
304304

@@ -342,7 +342,7 @@ fun main() = runBlocking {
342342

343343
</div>
344344

345-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-07.kt).
345+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-07.kt).
346346
347347
There is no longer an exception when running this code:
348348

Diff for: docs/channels.md

+10-10
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ fun main() = runBlocking {
5252

5353
</div>
5454

55-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-01.kt).
55+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-01.kt).
5656
5757
The output of this code is:
5858

@@ -99,7 +99,7 @@ fun main() = runBlocking {
9999

100100
</div>
101101

102-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-02.kt).
102+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-02.kt).
103103
104104
<!--- TEST
105105
1
@@ -141,7 +141,7 @@ fun main() = runBlocking {
141141

142142
</div>
143143

144-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-03.kt).
144+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-03.kt).
145145
146146
<!--- TEST
147147
1
@@ -214,7 +214,7 @@ fun CoroutineScope.square(numbers: ReceiveChannel<Int>): ReceiveChannel<Int> = p
214214

215215
</div>
216216

217-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-04.kt).
217+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-04.kt).
218218
219219
<!--- TEST
220220
1
@@ -305,7 +305,7 @@ fun CoroutineScope.filter(numbers: ReceiveChannel<Int>, prime: Int) = produce<In
305305

306306
</div>
307307

308-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-05.kt).
308+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-05.kt).
309309
310310
The output of this code is:
311311

@@ -408,7 +408,7 @@ fun CoroutineScope.launchProcessor(id: Int, channel: ReceiveChannel<Int>) = laun
408408

409409
</div>
410410

411-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-06.kt).
411+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-06.kt).
412412
413413
The output will be similar to the the following one, albeit the processor ids that receive
414414
each specific integer may be different:
@@ -488,7 +488,7 @@ suspend fun sendString(channel: SendChannel<String>, s: String, time: Long) {
488488

489489
</div>
490490

491-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-07.kt).
491+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-07.kt).
492492
493493
The output is:
494494

@@ -540,7 +540,7 @@ fun main() = runBlocking<Unit> {
540540

541541
</div>
542542

543-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-08.kt).
543+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-08.kt).
544544
545545
It prints "sending" _five_ times using a buffered channel with capacity of _four_:
546546

@@ -595,7 +595,7 @@ suspend fun player(name: String, table: Channel<Ball>) {
595595

596596
</div>
597597

598-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-09.kt).
598+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-09.kt).
599599
600600
The "ping" coroutine is started first, so it is the first one to receive the ball. Even though "ping"
601601
coroutine immediately starts receiving the ball again after sending it back to the table, the ball gets
@@ -658,7 +658,7 @@ fun main() = runBlocking<Unit> {
658658

659659
</div>
660660

661-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-10.kt).
661+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-channel-10.kt).
662662
663663
It prints following lines:
664664

Diff for: docs/composing-suspending-functions.md

+6-6
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ suspend fun doSomethingUsefulTwo(): Int {
8181

8282
</div>
8383

84-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-01.kt).
84+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-01.kt).
8585
8686
It produces something like this:
8787

@@ -134,7 +134,7 @@ suspend fun doSomethingUsefulTwo(): Int {
134134

135135
</div>
136136

137-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-02.kt).
137+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-02.kt).
138138
139139
It produces something like this:
140140

@@ -188,7 +188,7 @@ suspend fun doSomethingUsefulTwo(): Int {
188188

189189
</div>
190190

191-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-03.kt).
191+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-03.kt).
192192
193193
It produces something like this:
194194

@@ -285,7 +285,7 @@ suspend fun doSomethingUsefulTwo(): Int {
285285

286286
</div>
287287

288-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-04.kt).
288+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-04.kt).
289289
290290
<!--- TEST ARBITRARY_TIME
291291
The answer is 42
@@ -361,7 +361,7 @@ suspend fun doSomethingUsefulTwo(): Int {
361361

362362
</div>
363363

364-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-05.kt).
364+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-05.kt).
365365
366366
We still have concurrent execution of both operations, as evident from the output of the above `main` function:
367367

@@ -408,7 +408,7 @@ suspend fun failedConcurrentSum(): Int = coroutineScope {
408408

409409
</div>
410410

411-
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-06.kt).
411+
> You can get the full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-compose-06.kt).
412412
413413
Note how both the first `async` and the awaiting parent are cancelled on failure of one of the children
414414
(namely, `two`):

0 commit comments

Comments
 (0)