@@ -47,7 +47,7 @@ Run the following code:
4747import kotlinx.coroutines.*
4848
4949fun main () {
50- GlobalScope .launch { // launch new coroutine in background and continue
50+ GlobalScope .launch { // launch a new coroutine in background and continue
5151 delay(1000L ) // non-blocking delay for 1 second (default time unit is ms)
5252 println (" World!" ) // print after delay
5353 }
@@ -58,7 +58,7 @@ fun main() {
5858
5959</div >
6060
61- > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-01.kt )
61+ > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-01.kt ) .
6262
6363You will see the following result:
6464
@@ -98,7 +98,7 @@ Let's be explicit about blocking using [runBlocking] coroutine builder:
9898import kotlinx.coroutines.*
9999
100100fun main () {
101- GlobalScope .launch { // launch new coroutine in background and continue
101+ GlobalScope .launch { // launch a new coroutine in background and continue
102102 delay(1000L )
103103 println (" World!" )
104104 }
@@ -111,15 +111,15 @@ fun main() {
111111
112112</div >
113113
114- > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-02.kt )
114+ > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-02.kt ) .
115115
116116<!-- - TEST
117117Hello,
118118World!
119119-->
120120
121121The result is the same, but this code uses only non-blocking [ delay] .
122- The main thread, that invokes ` runBlocking ` , _ blocks_ until the coroutine inside ` runBlocking ` completes.
122+ The main thread invoking ` runBlocking ` _ blocks_ until the coroutine inside ` runBlocking ` completes.
123123
124124This example can be also rewritten in a more idiomatic way, using ` runBlocking ` to wrap
125125the execution of the main function:
@@ -130,7 +130,7 @@ the execution of the main function:
130130import kotlinx.coroutines.*
131131
132132fun main () = runBlocking<Unit > { // start main coroutine
133- GlobalScope .launch { // launch new coroutine in background and continue
133+ GlobalScope .launch { // launch a new coroutine in background and continue
134134 delay(1000L )
135135 println (" World!" )
136136 }
@@ -141,7 +141,7 @@ fun main() = runBlocking<Unit> { // start main coroutine
141141
142142</div >
143143
144- > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-02b.kt )
144+ > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-02b.kt ) .
145145
146146<!-- - TEST
147147Hello,
@@ -151,7 +151,7 @@ World!
151151Here ` runBlocking<Unit> { ... } ` works as an adaptor that is used to start the top-level main coroutine.
152152We explicitly specify its ` Unit ` return type, because a well-formed ` main ` function in Kotlin has to return ` Unit ` .
153153
154- This is also a way to write unit- tests for suspending functions:
154+ This is also a way to write unit tests for suspending functions:
155155
156156<!-- - INCLUDE
157157import kotlinx.coroutines.*
@@ -184,7 +184,7 @@ import kotlinx.coroutines.*
184184
185185fun main () = runBlocking {
186186// sampleStart
187- val job = GlobalScope .launch { // launch new coroutine and keep a reference to its Job
187+ val job = GlobalScope .launch { // launch a new coroutine and keep a reference to its Job
188188 delay(1000L )
189189 println (" World!" )
190190 }
@@ -196,7 +196,7 @@ fun main() = runBlocking {
196196
197197</div >
198198
199- > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-03.kt )
199+ > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-03.kt ) .
200200
201201<!-- - TEST
202202Hello,
@@ -209,11 +209,11 @@ the background job in any way. Much better.
209209### Structured concurrency
210210
211211There is still something to be desired for practical usage of coroutines.
212- When we use ` GlobalScope.launch ` we create a top-level coroutine. Even though it is light-weight, it still
212+ When we use ` GlobalScope.launch ` , we create a top-level coroutine. Even though it is light-weight, it still
213213consumes some memory resources while it runs. If we forget to keep a reference to the newly launched
214214coroutine it still runs. What if the code in the coroutine hangs (for example, we erroneously
215215delay for too long), what if we launched too many coroutines and ran out of memory?
216- Having to manually keep a reference to all the launched coroutines and [ join] [ Job.join ] them is error-prone.
216+ Having to manually keep references to all the launched coroutines and [ join] [ Job.join ] them is error-prone.
217217
218218There is a better solution. We can use structured concurrency in our code.
219219Instead of launching coroutines in the [ GlobalScope] , just like we usually do with threads (threads are always global),
@@ -231,7 +231,7 @@ in its scope complete. Thus, we can make our example simpler:
231231import kotlinx.coroutines.*
232232
233233fun main () = runBlocking { // this: CoroutineScope
234- launch { // launch new coroutine in the scope of runBlocking
234+ launch { // launch a new coroutine in the scope of runBlocking
235235 delay(1000L )
236236 println (" World!" )
237237 }
@@ -241,7 +241,7 @@ fun main() = runBlocking { // this: CoroutineScope
241241
242242</div >
243243
244- > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-03s.kt )
244+ > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-03s.kt ) .
245245
246246<!-- - TEST
247247Hello,
@@ -250,7 +250,7 @@ World!
250250
251251### Scope builder
252252In addition to the coroutine scope provided by different builders, it is possible to declare your own scope using
253- [ coroutineScope] builder. It creates new coroutine scope and does not complete until all launched children
253+ [ coroutineScope] builder. It creates a coroutine scope and does not complete until all launched children
254254complete. The main difference between [ runBlocking] and [ coroutineScope] is that the latter does not block the current thread
255255while waiting for all children to complete.
256256
@@ -265,23 +265,23 @@ fun main() = runBlocking { // this: CoroutineScope
265265 println (" Task from runBlocking" )
266266 }
267267
268- coroutineScope { // Creates a new coroutine scope
268+ coroutineScope { // Creates a coroutine scope
269269 launch {
270270 delay(500L )
271271 println (" Task from nested launch" )
272272 }
273273
274274 delay(100L )
275- println (" Task from coroutine scope" ) // This line will be printed before nested launch
275+ println (" Task from coroutine scope" ) // This line will be printed before the nested launch
276276 }
277277
278- println (" Coroutine scope is over" ) // This line is not printed until nested launch completes
278+ println (" Coroutine scope is over" ) // This line is not printed until the nested launch completes
279279}
280280```
281281
282282</div >
283283
284- > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-04.kt )
284+ > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-04.kt ) .
285285
286286<!-- - TEST
287287Task from coroutine scope
@@ -317,7 +317,7 @@ suspend fun doWorld() {
317317
318318</div >
319319
320- > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-05.kt )
320+ > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-05.kt ) .
321321
322322<!-- - TEST
323323Hello,
@@ -328,10 +328,10 @@ World!
328328But what if the extracted function contains a coroutine builder which is invoked on the current scope?
329329In this case ` suspend ` modifier on the extracted function is not enough. Making ` doWorld ` extension
330330method on ` CoroutineScope ` is one of the solutions, but it may not always be applicable as it does not make API clearer.
331- Idiomatic solution is to have either explicit ` CoroutineScope ` as a field in a class containing target function
332- or implicit when outer class implements ` CoroutineScope ` .
331+ The idiomatic solution is to have either an explicit ` CoroutineScope ` as a field in a class containing the target function
332+ or an implicit one when the outer class implements ` CoroutineScope ` .
333333As a last resort, [ CoroutineScope(coroutineContext)] [ CoroutineScope() ] can be used, but such approach is structurally unsafe
334- because you no longer have control on the scope this method is executed . Only private API can use this builder.
334+ because you no longer have control on the scope of execution of this method . Only private APIs can use this builder.
335335
336336### Coroutines ARE light-weight
337337
@@ -354,7 +354,7 @@ fun main() = runBlocking {
354354
355355</div >
356356
357- > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt )
357+ > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-06.kt ) .
358358
359359<!-- - TEST lines.size == 1 && lines[0] == ".".repeat(100_000) -->
360360
@@ -386,7 +386,7 @@ fun main() = runBlocking {
386386
387387</div >
388388
389- > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-07.kt )
389+ > You can get full code [ here] ( ../kotlinx-coroutines-core/jvm/test/guide/example-basic-07.kt ) .
390390
391391You can run and see that it prints three lines and terminates:
392392
0 commit comments