Skip to content

Commit 563b7e5

Browse files
Inegoelizarov
authored andcommitted
Fix more typos; reword some phrases; add "job:" to textual output in … (#1154)
* Fix more typos; reword some phrases; add "job:" to textual output in examples to better differentiate between the "main" and "job" coroutines * Gradle knit
1 parent d0f6229 commit 563b7e5

16 files changed

+135
-136
lines changed

coroutines-guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ The main coroutines guide has moved to the [docs folder](docs/coroutines-guide.m
1717
* <a name='cancelling-coroutine-execution'></a>[Cancelling coroutine execution](docs/cancellation-and-timeouts.md#cancelling-coroutine-execution)
1818
* <a name='cancellation-is-cooperative'></a>[Cancellation is cooperative](docs/cancellation-and-timeouts.md#cancellation-is-cooperative)
1919
* <a name='making-computation-code-cancellable'></a>[Making computation code cancellable](docs/cancellation-and-timeouts.md#making-computation-code-cancellable)
20-
* <a name='closing-resources-with-finally'></a>[Closing resources with finally](docs/cancellation-and-timeouts.md#closing-resources-with-finally)
20+
* <a name='closing-resources-with-`finally`'></a>[Closing resources with `finally`](docs/cancellation-and-timeouts.md#closing-resources-with-`finally`)
2121
* <a name='run-non-cancellable-block'></a>[Run non-cancellable block](docs/cancellation-and-timeouts.md#run-non-cancellable-block)
2222
* <a name='timeout'></a>[Timeout](docs/cancellation-and-timeouts.md#timeout)
2323
<!--- TOC_REF docs/composing-suspending-functions.md -->

docs/cancellation-and-timeouts.md

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class CancellationTimeOutsGuideTest {
2323
* [Cancelling coroutine execution](#cancelling-coroutine-execution)
2424
* [Cancellation is cooperative](#cancellation-is-cooperative)
2525
* [Making computation code cancellable](#making-computation-code-cancellable)
26-
* [Closing resources with finally](#closing-resources-with-finally)
26+
* [Closing resources with `finally`](#closing-resources-with-`finally`)
2727
* [Run non-cancellable block](#run-non-cancellable-block)
2828
* [Timeout](#timeout)
2929

@@ -49,7 +49,7 @@ fun main() = runBlocking {
4949
//sampleStart
5050
val job = launch {
5151
repeat(1000) { i ->
52-
println("I'm sleeping $i ...")
52+
println("job: I'm sleeping $i ...")
5353
delay(500L)
5454
}
5555
}
@@ -69,9 +69,9 @@ fun main() = runBlocking {
6969
It produces the following output:
7070

7171
```text
72-
I'm sleeping 0 ...
73-
I'm sleeping 1 ...
74-
I'm sleeping 2 ...
72+
job: I'm sleeping 0 ...
73+
job: I'm sleeping 1 ...
74+
job: I'm sleeping 2 ...
7575
main: I'm tired of waiting!
7676
main: Now I can quit.
7777
```
@@ -104,7 +104,7 @@ fun main() = runBlocking {
104104
while (i < 5) { // computation loop, just wastes CPU
105105
// print a message twice a second
106106
if (System.currentTimeMillis() >= nextPrintTime) {
107-
println("I'm sleeping ${i++} ...")
107+
println("job: I'm sleeping ${i++} ...")
108108
nextPrintTime += 500L
109109
}
110110
}
@@ -125,12 +125,12 @@ Run it to see that it continues to print "I'm sleeping" even after cancellation
125125
until the job completes by itself after five iterations.
126126

127127
<!--- TEST
128-
I'm sleeping 0 ...
129-
I'm sleeping 1 ...
130-
I'm sleeping 2 ...
128+
job: I'm sleeping 0 ...
129+
job: I'm sleeping 1 ...
130+
job: I'm sleeping 2 ...
131131
main: I'm tired of waiting!
132-
I'm sleeping 3 ...
133-
I'm sleeping 4 ...
132+
job: I'm sleeping 3 ...
133+
job: I'm sleeping 4 ...
134134
main: Now I can quit.
135135
-->
136136

@@ -156,7 +156,7 @@ fun main() = runBlocking {
156156
while (isActive) { // cancellable computation loop
157157
// print a message twice a second
158158
if (System.currentTimeMillis() >= nextPrintTime) {
159-
println("I'm sleeping ${i++} ...")
159+
println("job: I'm sleeping ${i++} ...")
160160
nextPrintTime += 500L
161161
}
162162
}
@@ -173,22 +173,22 @@ fun main() = runBlocking {
173173

174174
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-03.kt).
175175
176-
As you can see, now this loop is cancelled. [isActive] is an extension property that is
177-
available inside the code of coroutine via [CoroutineScope] object.
176+
As you can see, now this loop is cancelled. [isActive] is an extension property
177+
available inside the coroutine via the [CoroutineScope] object.
178178

179179
<!--- TEST
180-
I'm sleeping 0 ...
181-
I'm sleeping 1 ...
182-
I'm sleeping 2 ...
180+
job: I'm sleeping 0 ...
181+
job: I'm sleeping 1 ...
182+
job: I'm sleeping 2 ...
183183
main: I'm tired of waiting!
184184
main: Now I can quit.
185185
-->
186186

187-
### Closing resources with finally
187+
### Closing resources with `finally`
188188

189189
Cancellable suspending functions throw [CancellationException] on cancellation which can be handled in
190-
a usual way. For example, `try {...} finally {...}` expression and Kotlin `use` function execute their
191-
finalization actions normally when coroutine is cancelled:
190+
the usual way. For example, `try {...} finally {...}` expression and Kotlin `use` function execute their
191+
finalization actions normally when a coroutine is cancelled:
192192

193193

194194
<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
@@ -201,11 +201,11 @@ fun main() = runBlocking {
201201
val job = launch {
202202
try {
203203
repeat(1000) { i ->
204-
println("I'm sleeping $i ...")
204+
println("job: I'm sleeping $i ...")
205205
delay(500L)
206206
}
207207
} finally {
208-
println("I'm running finally")
208+
println("job: I'm running finally")
209209
}
210210
}
211211
delay(1300L) // delay a bit
@@ -220,15 +220,15 @@ fun main() = runBlocking {
220220

221221
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-04.kt).
222222
223-
Both [join][Job.join] and [cancelAndJoin] wait for all the finalization actions to complete,
223+
Both [join][Job.join] and [cancelAndJoin] wait for all finalization actions to complete,
224224
so the example above produces the following output:
225225

226226
```text
227-
I'm sleeping 0 ...
228-
I'm sleeping 1 ...
229-
I'm sleeping 2 ...
227+
job: I'm sleeping 0 ...
228+
job: I'm sleeping 1 ...
229+
job: I'm sleeping 2 ...
230230
main: I'm tired of waiting!
231-
I'm running finally
231+
job: I'm running finally
232232
main: Now I can quit.
233233
```
234234

@@ -240,7 +240,7 @@ Any attempt to use a suspending function in the `finally` block of the previous
240240
[CancellationException], because the coroutine running this code is cancelled. Usually, this is not a
241241
problem, since all well-behaving closing operations (closing a file, cancelling a job, or closing any kind of a
242242
communication channel) are usually non-blocking and do not involve any suspending functions. However, in the
243-
rare case when you need to suspend in the cancelled coroutine you can wrap the corresponding code in
243+
rare case when you need to suspend in a cancelled coroutine you can wrap the corresponding code in
244244
`withContext(NonCancellable) {...}` using [withContext] function and [NonCancellable] context as the following example shows:
245245

246246
<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">
@@ -253,14 +253,14 @@ fun main() = runBlocking {
253253
val job = launch {
254254
try {
255255
repeat(1000) { i ->
256-
println("I'm sleeping $i ...")
256+
println("job: I'm sleeping $i ...")
257257
delay(500L)
258258
}
259259
} finally {
260260
withContext(NonCancellable) {
261-
println("I'm running finally")
261+
println("job: I'm running finally")
262262
delay(1000L)
263-
println("And I've just delayed for 1 sec because I'm non-cancellable")
263+
println("job: And I've just delayed for 1 sec because I'm non-cancellable")
264264
}
265265
}
266266
}
@@ -277,18 +277,18 @@ fun main() = runBlocking {
277277
> You can get full code [here](../kotlinx-coroutines-core/jvm/test/guide/example-cancel-05.kt).
278278
279279
<!--- TEST
280-
I'm sleeping 0 ...
281-
I'm sleeping 1 ...
282-
I'm sleeping 2 ...
280+
job: I'm sleeping 0 ...
281+
job: I'm sleeping 1 ...
282+
job: I'm sleeping 2 ...
283283
main: I'm tired of waiting!
284-
I'm running finally
285-
And I've just delayed for 1 sec because I'm non-cancellable
284+
job: I'm running finally
285+
job: And I've just delayed for 1 sec because I'm non-cancellable
286286
main: Now I can quit.
287287
-->
288288

289289
### Timeout
290290

291-
The most obvious reason to cancel coroutine execution in practice
291+
The most obvious practical reason to cancel execution of a coroutine
292292
is because its execution time has exceeded some timeout.
293293
While you can manually track the reference to the corresponding [Job] and launch a separate coroutine to cancel
294294
the tracked one after delay, there is a ready to use [withTimeout] function that does it.
@@ -331,10 +331,10 @@ We have not seen its stack trace printed on the console before. That is because
331331
inside a cancelled coroutine `CancellationException` is considered to be a normal reason for coroutine completion.
332332
However, in this example we have used `withTimeout` right inside the `main` function.
333333

334-
Because cancellation is just an exception, all the resources are closed in a usual way.
335-
You can wrap the code with timeout in `try {...} catch (e: TimeoutCancellationException) {...}` block if
336-
you need to do some additional action specifically on any kind of timeout or use [withTimeoutOrNull] function
337-
that is similar to [withTimeout], but returns `null` on timeout instead of throwing an exception:
334+
Since cancellation is just an exception, all resources are closed in the usual way.
335+
You can wrap the code with timeout in a `try {...} catch (e: TimeoutCancellationException) {...}` block if
336+
you need to do some additional action specifically on any kind of timeout or use the [withTimeoutOrNull] function
337+
that is similar to [withTimeout] but returns `null` on timeout instead of throwing an exception:
338338

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

kotlinx-coroutines-core/common/src/Annotations.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public annotation class ExperimentalCoroutinesApi
2424
* Its API and semantics can and will be changed in next releases.
2525
*
2626
* Feature preview can be used to evaluate its real-world strengths and weaknesses, gather and provide feedback.
27-
* According to the feedback, [Flow] will be refined on its road to stabilization and promotion to stable API.
27+
* According to the feedback, [Flow] will be refined on its road to stabilization and promotion to a stable API.
2828
*/
2929
@MustBeDocumented
3030
@Retention(value = AnnotationRetention.BINARY)
@@ -44,7 +44,7 @@ public annotation class ObsoleteCoroutinesApi
4444

4545
/**
4646
* Marks declarations that are **internal** in coroutines API, which means that should not be used outside of
47-
* `kotlinx.coroutines`, because their signatures and semantics will be changing between release without any
47+
* `kotlinx.coroutines`, because their signatures and semantics will change between future releases without any
4848
* warnings and without providing any migration aids.
4949
*/
5050
@Retention(value = AnnotationRetention.BINARY)

kotlinx-coroutines-core/common/src/CoroutineScope.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public interface CoroutineScope {
5151
/**
5252
* The context of this scope.
5353
* Context is encapsulated by the scope and used for implementation of coroutine builders that are extensions on the scope.
54-
* Accessing this property in general code is not recommended for any purposes except accessing [Job] instance for advanced usages.
54+
* Accessing this property in general code is not recommended for any purposes except accessing the [Job] instance for advanced usages.
5555
*
5656
* By convention, should contain an instance of a [job][Job] to enforce structured concurrency.
5757
*/
@@ -91,7 +91,7 @@ public operator fun CoroutineScope.plus(context: CoroutineContext): CoroutineSco
9191
public fun MainScope(): CoroutineScope = ContextScope(SupervisorJob() + Dispatchers.Main)
9292

9393
/**
94-
* Returns `true` when current [Job] is still active (has not completed and was not cancelled yet).
94+
* Returns `true` when the current [Job] is still active (has not completed and was not cancelled yet).
9595
*
9696
* Check this property in long-running computation loops to support cancellation:
9797
* ```

kotlinx-coroutines-core/common/src/Dispatchers.common.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,36 +13,36 @@ public expect object Dispatchers {
1313
/**
1414
* The default [CoroutineDispatcher] that is used by all standard builders like
1515
* [launch][CoroutineScope.launch], [async][CoroutineScope.async], etc
16-
* if no dispatcher nor any other [ContinuationInterceptor] is specified in their context.
16+
* if neither a dispatcher nor any other [ContinuationInterceptor] is specified in their context.
1717
*
18-
* It is backed by a shared pool of threads on JVM. By default, the maximal number of threads used
19-
* by this dispatcher is equal to the number CPU cores, but is at least two.
18+
* It is backed by a shared pool of threads on JVM. By default, the maximum number of threads used
19+
* by this dispatcher is equal to the number of CPU cores, but is at least two.
2020
*/
2121
public val Default: CoroutineDispatcher
2222

2323
/**
2424
* A coroutine dispatcher that is confined to the Main thread operating with UI objects.
25-
* Usually such dispatcher is single-threaded.
25+
* Usually such dispatchers are single-threaded.
2626
*
27-
* Access to this property may throw [IllegalStateException] if no main dispatchers are present in the classpath.
27+
* Access to this property may throw an [IllegalStateException] if no main dispatchers are present in the classpath.
2828
*
2929
* Depending on platform and classpath it can be mapped to different dispatchers:
30-
* - On JS and Native it is equivalent of [Default] dispatcher.
31-
* - On JVM it either Android main thread dispatcher, JavaFx or Swing EDT dispatcher. It is chosen by
30+
* - On JS and Native it is equivalent to the [Default] dispatcher.
31+
* - On JVM it either the Android main thread dispatcher, JavaFx or Swing EDT dispatcher. It is chosen by the
3232
* [`ServiceLoader`](https://docs.oracle.com/javase/8/docs/api/java/util/ServiceLoader.html).
3333
*
34-
* In order to work with `Main` dispatcher, following artifact should be added to project runtime dependencies:
35-
* - `kotlinx-coroutines-android` for Android Main thread dispatcher
36-
* - `kotlinx-coroutines-javafx` for JavaFx Application thread dispatcher
37-
* - `kotlinx-coroutines-swing` for Swing EDT dispatcher
34+
* In order to work with the `Main` dispatcher, the following artifact should be added to the project runtime dependencies:
35+
* - `kotlinx-coroutines-android` &mdash; for Android Main thread dispatcher
36+
* - `kotlinx-coroutines-javafx` &mdash; for JavaFx Application thread dispatcher
37+
* - `kotlinx-coroutines-swing` &mdash; for Swing EDT dispatcher
3838
*
39-
* Implementation note: [MainCoroutineDispatcher.immediate] is not supported on Native and JS platforms.
39+
* Implementation note: [MainCoroutineDispatcher.immediate] is not supported on the Native and JS platforms.
4040
*/
4141
public val Main: MainCoroutineDispatcher
4242

4343
/**
4444
* A coroutine dispatcher that is not confined to any specific thread.
45-
* It executes initial continuation of the coroutine in the current call-frame
45+
* It executes the initial continuation of a coroutine in the current call-frame
4646
* and lets the coroutine resume in whatever thread that is used by the corresponding suspending function, without
4747
* mandating any specific threading policy. Nested coroutines launched in this dispatcher form an event-loop to avoid
4848
* stack overflows.
@@ -64,12 +64,12 @@ public expect object Dispatchers {
6464
* println("Done")
6565
* ```
6666
* Can print both "1 2 3" and "1 3 2", this is an implementation detail that can be changed.
67-
* But it is guaranteed that "Done" will be printed only when both `withContext` are completed.
67+
* But it is guaranteed that "Done" will be printed only when both `withContext` calls are completed.
6868
*
6969
* Note that if you need your coroutine to be confined to a particular thread or a thread-pool after resumption,
7070
* but still want to execute it in the current call-frame until its first suspension, then you can use
7171
* an optional [CoroutineStart] parameter in coroutine builders like
72-
* [launch][CoroutineScope.launch] and [async][CoroutineScope.async] setting it to the
72+
* [launch][CoroutineScope.launch] and [async][CoroutineScope.async] setting it to
7373
* the value of [CoroutineStart.UNDISPATCHED].
7474
*/
7575
public val Unconfined: CoroutineDispatcher

kotlinx-coroutines-core/common/src/Timeout.kt

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,16 @@ import kotlin.coroutines.intrinsics.*
1212
import kotlin.jvm.*
1313

1414
/**
15-
* Runs a given suspending [block] of code inside a coroutine with a specified timeout and throws
16-
* [TimeoutCancellationException] if timeout was exceeded.
15+
* Runs a given suspending [block] of code inside a coroutine with a specified [timeout][timeMillis] and throws
16+
* a [TimeoutCancellationException] if the timeout was exceeded.
1717
*
1818
* The code that is executing inside the [block] is cancelled on timeout and the active or next invocation of
19-
* cancellable suspending function inside the block throws [TimeoutCancellationException].
19+
* the cancellable suspending function inside the block throws a [TimeoutCancellationException].
2020
*
21-
* The sibling function that does not throw exception on timeout is [withTimeoutOrNull].
22-
* Note that timeout action can be specified for [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
21+
* The sibling function that does not throw an exception on timeout is [withTimeoutOrNull].
22+
* Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
2323
*
24-
* Implementation note: how exactly time is tracked is an implementation detail of [CoroutineDispatcher] in the context.
24+
* Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
2525
*
2626
* @param timeMillis timeout time in milliseconds.
2727
*/
@@ -33,16 +33,16 @@ public suspend fun <T> withTimeout(timeMillis: Long, block: suspend CoroutineSco
3333
}
3434

3535
/**
36-
* Runs a given suspending block of code inside a coroutine with a specified timeout and returns
36+
* Runs a given suspending block of code inside a coroutine with a specified [timeout][timeMillis] and returns
3737
* `null` if this timeout was exceeded.
3838
*
3939
* The code that is executing inside the [block] is cancelled on timeout and the active or next invocation of
40-
* cancellable suspending function inside the block throws [TimeoutCancellationException].
40+
* cancellable suspending function inside the block throws a [TimeoutCancellationException].
4141
*
42-
* The sibling function that throws exception on timeout is [withTimeout].
43-
* Note that timeout action can be specified for [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
42+
* The sibling function that throws an exception on timeout is [withTimeout].
43+
* Note that the timeout action can be specified for a [select] invocation with [onTimeout][SelectBuilder.onTimeout] clause.
4444
*
45-
* Implementation note: how exactly time is tracked is an implementation detail of [CoroutineDispatcher] in the context.
45+
* Implementation note: how the time is tracked exactly is an implementation detail of the context's [CoroutineDispatcher].
4646
*
4747
* @param timeMillis timeout time in milliseconds.
4848
*/
@@ -57,7 +57,7 @@ public suspend fun <T> withTimeoutOrNull(timeMillis: Long, block: suspend Corout
5757
setupTimeout<T?, T?>(timeoutCoroutine, block)
5858
}
5959
} catch (e: TimeoutCancellationException) {
60-
// Return null iff it's our exception, otherwise propagate it upstream (e.g. in case of nested withTimeouts)
60+
// Return null if it's our exception, otherwise propagate it upstream (e.g. in case of nested withTimeouts)
6161
if (e.coroutine === coroutine) {
6262
return null
6363
}
@@ -73,8 +73,8 @@ private fun <U, T: U> setupTimeout(
7373
val cont = coroutine.uCont
7474
val context = cont.context
7575
coroutine.disposeOnCompletion(context.delay.invokeOnTimeout(coroutine.time, coroutine))
76-
// restart block using new coroutine with new job,
77-
// however start it as undispatched coroutine, because we are already in the proper context
76+
// restart the block using a new coroutine with a new job,
77+
// however, start it undispatched, because we already are in the proper context
7878
return coroutine.startUndispatchedOrReturnIgnoreTimeout(coroutine, block)
7979
}
8080

@@ -114,7 +114,7 @@ public class TimeoutCancellationException internal constructor(
114114
@JvmField internal val coroutine: Job?
115115
) : CancellationException(message) {
116116
/**
117-
* Creates timeout exception with a given message.
117+
* Creates a timeout exception with the given message.
118118
* This constructor is needed for exception stack-traces recovery.
119119
*/
120120
@Suppress("UNUSED")

0 commit comments

Comments
 (0)