@@ -43,6 +43,8 @@ This section covers basic coroutine concepts.
43
43
44
44
Run the following code:
45
45
46
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
47
+
46
48
``` kotlin
47
49
fun main (args : Array <String >) {
48
50
GlobalScope .launch { // launch new coroutine in background and continue
@@ -54,6 +56,8 @@ fun main(args: Array<String>) {
54
56
}
55
57
```
56
58
59
+ </div >
60
+
57
61
> You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-01.kt )
58
62
59
63
Run this code:
@@ -88,6 +92,8 @@ The first example mixes _non-blocking_ `delay(...)` and _blocking_ `Thread.sleep
88
92
It is easy to get lost which one is blocking and which one is not.
89
93
Let's be explicit about blocking using [ runBlocking] coroutine builder:
90
94
95
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
96
+
91
97
``` kotlin
92
98
fun main (args : Array <String >) {
93
99
GlobalScope .launch { // launch new coroutine in background and continue
@@ -101,6 +107,8 @@ fun main(args: Array<String>) {
101
107
}
102
108
```
103
109
110
+ </div >
111
+
104
112
> You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-02.kt )
105
113
106
114
<!-- - TEST
@@ -114,6 +122,8 @@ The main thread, that invokes `runBlocking`, _blocks_ until the coroutine inside
114
122
This example can be also rewritten in a more idiomatic way, using ` runBlocking ` to wrap
115
123
the execution of the main function:
116
124
125
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
126
+
117
127
``` kotlin
118
128
fun main (args : Array <String >) = runBlocking { // start main coroutine
119
129
GlobalScope .launch { // launch new coroutine in background and continue
@@ -125,6 +135,8 @@ fun main(args: Array<String>) = runBlocking { // start main coroutine
125
135
}
126
136
```
127
137
138
+ </div >
139
+
128
140
> You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-02b.kt )
129
141
130
142
<!-- - TEST
@@ -136,6 +148,8 @@ Here `runBlocking<Unit> { ... }` works as an adaptor that is used to start the t
136
148
We explicitly specify its ` Unit ` return type, because a well-formed ` main ` function in Kotlin has to return ` Unit ` .
137
149
138
150
This is also a way to write unit-tests for suspending functions:
151
+
152
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
139
153
140
154
``` kotlin
141
155
class MyTest {
@@ -146,13 +160,17 @@ class MyTest {
146
160
}
147
161
```
148
162
163
+ </div >
164
+
149
165
<!-- - CLEAR -->
150
166
151
167
### Waiting for a job
152
168
153
169
Delaying for a time while another coroutine is working is not a good approach. Let's explicitly
154
170
wait (in a non-blocking way) until the background [ Job] that we have launched is complete:
155
171
172
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
173
+
156
174
``` kotlin
157
175
fun main (args : Array <String >) = runBlocking {
158
176
val job = GlobalScope .launch { // launch new coroutine and keep a reference to its Job
@@ -164,6 +182,8 @@ fun main(args: Array<String>) = runBlocking {
164
182
}
165
183
```
166
184
185
+ </div >
186
+
167
187
> You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-03.kt )
168
188
169
189
<!-- - TEST
@@ -193,6 +213,8 @@ We can launch coroutines in this scope without having to `join` them explicitly,
193
213
an outer coroutine (` runBlocking ` in our example) does not complete until all the coroutines launched
194
214
in its scope complete. Thus, we can make our example simpler:
195
215
216
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
217
+
196
218
``` kotlin
197
219
fun main (args : Array <String >) = runBlocking { // this: CoroutineScope
198
220
launch { // launch new coroutine in the scope of runBlocking
@@ -203,6 +225,8 @@ fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
203
225
}
204
226
```
205
227
228
+ </div >
229
+
206
230
> You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-03s.kt )
207
231
208
232
<!-- - TEST
@@ -216,6 +240,8 @@ In addition to the coroutine scope provided by different builders, it is possibl
216
240
complete. The main difference between [ runBlocking] and [ coroutineScope] is that the latter does not block the current thread
217
241
while waiting for all children to complete.
218
242
243
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
244
+
219
245
``` kotlin
220
246
fun main (args : Array <String >) = runBlocking { // this: CoroutineScope
221
247
launch {
@@ -237,6 +263,8 @@ fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
237
263
}
238
264
```
239
265
266
+ </div >
267
+
240
268
> You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-04.kt )
241
269
242
270
<!-- - TEST
@@ -254,6 +282,8 @@ That is your first _suspending function_. Suspending functions can be used insid
254
282
just like regular functions, but their additional feature is that they can, in turn,
255
283
use other suspending functions, like ` delay ` in this example, to _ suspend_ execution of a coroutine.
256
284
285
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
286
+
257
287
``` kotlin
258
288
fun main (args : Array <String >) = runBlocking {
259
289
launch { doWorld() }
@@ -267,6 +297,8 @@ suspend fun doWorld() {
267
297
}
268
298
```
269
299
300
+ </div >
301
+
270
302
> You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-05.kt )
271
303
272
304
<!-- - TEST
@@ -280,6 +312,8 @@ In this case `suspend` modifier on the extracted function is not enough. Making
280
312
method on ` CoroutineScope ` is one of the solutions, but it may not always be applicable as it does not make API clearer.
281
313
[ currentScope] builder comes to help: it inherits current [ CoroutineScope] from the coroutine context it is invoked.
282
314
315
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
316
+
283
317
``` kotlin
284
318
fun main (args : Array <String >) = runBlocking {
285
319
launchDoWorld()
@@ -294,6 +328,8 @@ suspend fun launchDoWorld() = currentScope {
294
328
}
295
329
```
296
330
331
+ </div >
332
+
297
333
> You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-05s.kt )
298
334
299
335
<!-- - TEST
@@ -305,6 +341,8 @@ World!
305
341
306
342
Run the following code:
307
343
344
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
345
+
308
346
``` kotlin
309
347
fun main (args : Array <String >) = runBlocking {
310
348
repeat(100_000 ) { // launch a lot of coroutines
@@ -316,6 +354,8 @@ fun main(args: Array<String>) = runBlocking {
316
354
}
317
355
```
318
356
357
+ </div >
358
+
319
359
> You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-06.kt )
320
360
321
361
<!-- - TEST lines.size == 1 && lines[0] == ".".repeat(100_000) -->
@@ -328,6 +368,8 @@ Now, try that with threads. What would happen? (Most likely your code will produ
328
368
The following code launches a long-running coroutine in [ GlobalScope] that prints "I'm sleeping" twice a second and then
329
369
returns from the main function after some delay:
330
370
371
+ <div class =" sample " markdown =" 1 " theme =" idea " data-highlight-only >
372
+
331
373
``` kotlin
332
374
fun main (args : Array <String >) = runBlocking {
333
375
GlobalScope .launch {
@@ -340,6 +382,8 @@ fun main(args: Array<String>) = runBlocking {
340
382
}
341
383
```
342
384
385
+ </div >
386
+
343
387
> You can get full code [ here] ( ../core/kotlinx-coroutines-core/test/guide/example-basic-07.kt )
344
388
345
389
You can run and see that it prints three lines and terminates:
0 commit comments