Skip to content

Commit 65e6c8c

Browse files
AlexanderPrendotaelizarov
authored andcommitted
Make examples runnable
* Also drop args from fun main
1 parent 56ee233 commit 65e6c8c

File tree

102 files changed

+828
-518
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

102 files changed

+828
-518
lines changed

core/kotlinx-coroutines-core/test/guide/example-basic-01.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package kotlinx.coroutines.guide.basic01
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) {
10+
fun main() {
1111
GlobalScope.launch { // launch new coroutine in background and continue
1212
delay(1000L) // non-blocking delay for 1 second (default time unit is ms)
1313
println("World!") // print after delay

core/kotlinx-coroutines-core/test/guide/example-basic-02.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package kotlinx.coroutines.guide.basic02
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) {
10+
fun main() {
1111
GlobalScope.launch { // launch new coroutine in background and continue
1212
delay(1000L)
1313
println("World!")

core/kotlinx-coroutines-core/test/guide/example-basic-02b.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package kotlinx.coroutines.guide.basic02b
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking<Unit> { // start main coroutine
10+
fun main() = runBlocking<Unit> { // start main coroutine
1111
GlobalScope.launch { // launch new coroutine in background and continue
1212
delay(1000L)
1313
println("World!")

core/kotlinx-coroutines-core/test/guide/example-basic-03.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ package kotlinx.coroutines.guide.basic03
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
11+
//sampleStart
1112
val job = GlobalScope.launch { // launch new coroutine and keep a reference to its Job
1213
delay(1000L)
1314
println("World!")
1415
}
1516
println("Hello,")
1617
job.join() // wait until child coroutine completes
18+
//sampleEnd
1719
}

core/kotlinx-coroutines-core/test/guide/example-basic-03s.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package kotlinx.coroutines.guide.basic03s
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
10+
fun main() = runBlocking { // this: CoroutineScope
1111
launch { // launch new coroutine in the scope of runBlocking
1212
delay(1000L)
1313
println("World!")

core/kotlinx-coroutines-core/test/guide/example-basic-04.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package kotlinx.coroutines.guide.basic04
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking { // this: CoroutineScope
10+
fun main() = runBlocking { // this: CoroutineScope
1111
launch {
1212
delay(200L)
1313
println("Task from runBlocking")

core/kotlinx-coroutines-core/test/guide/example-basic-05.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package kotlinx.coroutines.guide.basic05
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
1111
launch { doWorld() }
1212
println("Hello,")
1313
}

core/kotlinx-coroutines-core/test/guide/example-basic-06.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package kotlinx.coroutines.guide.basic06
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
1111
repeat(100_000) { // launch a lot of coroutines
1212
launch {
1313
delay(1000L)

core/kotlinx-coroutines-core/test/guide/example-basic-07.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@ package kotlinx.coroutines.guide.basic07
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
11+
//sampleStart
1112
GlobalScope.launch {
1213
repeat(1000) { i ->
1314
println("I'm sleeping $i ...")
1415
delay(500L)
1516
}
1617
}
1718
delay(1300L) // just quit after delay
19+
//sampleEnd
1820
}

core/kotlinx-coroutines-core/test/guide/example-cancel-01.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ package kotlinx.coroutines.guide.cancel01
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
11+
//sampleStart
1112
val job = launch {
1213
repeat(1000) { i ->
1314
println("I'm sleeping $i ...")
@@ -19,4 +20,5 @@ fun main(args: Array<String>) = runBlocking {
1920
job.cancel() // cancels the job
2021
job.join() // waits for job's completion
2122
println("main: Now I can quit.")
23+
//sampleEnd
2224
}

core/kotlinx-coroutines-core/test/guide/example-cancel-02.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ package kotlinx.coroutines.guide.cancel02
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
11+
//sampleStart
1112
val startTime = timeSource.currentTimeMillis()
1213
val job = launch(Dispatchers.Default) {
1314
var nextPrintTime = startTime
@@ -24,4 +25,5 @@ fun main(args: Array<String>) = runBlocking {
2425
println("main: I'm tired of waiting!")
2526
job.cancelAndJoin() // cancels the job and waits for its completion
2627
println("main: Now I can quit.")
28+
//sampleEnd
2729
}

core/kotlinx-coroutines-core/test/guide/example-cancel-03.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ package kotlinx.coroutines.guide.cancel03
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
11+
//sampleStart
1112
val startTime = timeSource.currentTimeMillis()
1213
val job = launch(Dispatchers.Default) {
1314
var nextPrintTime = startTime
@@ -24,4 +25,5 @@ fun main(args: Array<String>) = runBlocking {
2425
println("main: I'm tired of waiting!")
2526
job.cancelAndJoin() // cancels the job and waits for its completion
2627
println("main: Now I can quit.")
28+
//sampleEnd
2729
}

core/kotlinx-coroutines-core/test/guide/example-cancel-04.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ package kotlinx.coroutines.guide.cancel04
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
11+
//sampleStart
1112
val job = launch {
1213
try {
1314
repeat(1000) { i ->
@@ -22,4 +23,5 @@ fun main(args: Array<String>) = runBlocking {
2223
println("main: I'm tired of waiting!")
2324
job.cancelAndJoin() // cancels the job and waits for its completion
2425
println("main: Now I can quit.")
26+
//sampleEnd
2527
}

core/kotlinx-coroutines-core/test/guide/example-cancel-05.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ package kotlinx.coroutines.guide.cancel05
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
11+
//sampleStart
1112
val job = launch {
1213
try {
1314
repeat(1000) { i ->
@@ -26,4 +27,5 @@ fun main(args: Array<String>) = runBlocking {
2627
println("main: I'm tired of waiting!")
2728
job.cancelAndJoin() // cancels the job and waits for its completion
2829
println("main: Now I can quit.")
30+
//sampleEnd
2931
}

core/kotlinx-coroutines-core/test/guide/example-cancel-06.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ package kotlinx.coroutines.guide.cancel06
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
11+
//sampleStart
1112
withTimeout(1300L) {
1213
repeat(1000) { i ->
1314
println("I'm sleeping $i ...")
1415
delay(500L)
1516
}
1617
}
18+
//sampleEnd
1719
}

core/kotlinx-coroutines-core/test/guide/example-cancel-07.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ package kotlinx.coroutines.guide.cancel07
77

88
import kotlinx.coroutines.*
99

10-
fun main(args: Array<String>) = runBlocking {
10+
fun main() = runBlocking {
11+
//sampleStart
1112
val result = withTimeoutOrNull(1300L) {
1213
repeat(1000) { i ->
1314
println("I'm sleeping $i ...")
@@ -16,4 +17,5 @@ fun main(args: Array<String>) = runBlocking {
1617
"Done" // will get cancelled before it produces this result
1718
}
1819
println("Result is $result")
20+
//sampleEnd
1921
}

core/kotlinx-coroutines-core/test/guide/example-channel-01.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ package kotlinx.coroutines.guide.channel01
88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

11-
fun main(args: Array<String>) = runBlocking {
11+
fun main() = runBlocking {
12+
//sampleStart
1213
val channel = Channel<Int>()
1314
launch {
1415
// this might be heavy CPU-consuming computation or async logic, we'll just send five squares
@@ -17,4 +18,5 @@ fun main(args: Array<String>) = runBlocking {
1718
// here we print five received integers:
1819
repeat(5) { println(channel.receive()) }
1920
println("Done!")
21+
//sampleEnd
2022
}

core/kotlinx-coroutines-core/test/guide/example-channel-02.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ package kotlinx.coroutines.guide.channel02
88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

11-
fun main(args: Array<String>) = runBlocking {
11+
fun main() = runBlocking {
12+
//sampleStart
1213
val channel = Channel<Int>()
1314
launch {
1415
for (x in 1..5) channel.send(x * x)
@@ -17,4 +18,5 @@ fun main(args: Array<String>) = runBlocking {
1718
// here we print received values using `for` loop (until the channel is closed)
1819
for (y in channel) println(y)
1920
println("Done!")
21+
//sampleEnd
2022
}

core/kotlinx-coroutines-core/test/guide/example-channel-03.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ fun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
1212
for (x in 1..5) send(x * x)
1313
}
1414

15-
fun main(args: Array<String>) = runBlocking {
15+
fun main() = runBlocking {
16+
//sampleStart
1617
val squares = produceSquares()
1718
squares.consumeEach { println(it) }
1819
println("Done!")
20+
//sampleEnd
1921
}

core/kotlinx-coroutines-core/test/guide/example-channel-04.kt

+14-9
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,24 @@ package kotlinx.coroutines.guide.channel04
88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

11+
fun main() = runBlocking {
12+
//sampleStart
13+
val numbers = produceNumbers() // produces integers from 1 and on
14+
val squares = square(numbers) // squares integers
15+
for (i in 1..5) println(squares.receive()) // print first five
16+
println("Done!") // we are done
17+
coroutineContext.cancelChildren() // cancel children coroutines
18+
//sampleEnd
19+
}
20+
21+
fun CoroutineScope.produceSquares(): ReceiveChannel<Int> = produce {
22+
for (x in 1..5) send(x * x)
23+
}
24+
1125
fun CoroutineScope.produceNumbers() = produce<Int> {
1226
var x = 1
1327
while (true) send(x++) // infinite stream of integers starting from 1
1428
}
15-
1629
fun CoroutineScope.square(numbers: ReceiveChannel<Int>): ReceiveChannel<Int> = produce {
1730
for (x in numbers) send(x * x)
1831
}
19-
20-
fun main(args: Array<String>) = runBlocking {
21-
val numbers = produceNumbers() // produces integers from 1 and on
22-
val squares = square(numbers) // squares integers
23-
for (i in 1..5) println(squares.receive()) // print first five
24-
println("Done!") // we are done
25-
coroutineContext.cancelChildren() // cancel children coroutines
26-
}

core/kotlinx-coroutines-core/test/guide/example-channel-05.kt

+12-11
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,24 @@ package kotlinx.coroutines.guide.channel05
77

88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
10-
import kotlin.coroutines.*
1110

12-
fun CoroutineScope.numbersFrom(start: Int) = produce<Int> {
13-
var x = start
14-
while (true) send(x++) // infinite stream of integers from start
15-
}
16-
17-
fun CoroutineScope.filter(numbers: ReceiveChannel<Int>, prime: Int) = produce<Int> {
18-
for (x in numbers) if (x % prime != 0) send(x)
19-
}
20-
21-
fun main(args: Array<String>) = runBlocking {
11+
fun main() = runBlocking {
12+
//sampleStart
2213
var cur = numbersFrom(2)
2314
for (i in 1..10) {
2415
val prime = cur.receive()
2516
println(prime)
2617
cur = filter(cur, prime)
2718
}
2819
coroutineContext.cancelChildren() // cancel all children to let main finish
20+
//sampleEnd
21+
}
22+
23+
fun CoroutineScope.numbersFrom(start: Int) = produce<Int> {
24+
var x = start
25+
while (true) send(x++) // infinite stream of integers from start
26+
}
27+
28+
fun CoroutineScope.filter(numbers: ReceiveChannel<Int>, prime: Int) = produce<Int> {
29+
for (x in numbers) if (x % prime != 0) send(x)
2930
}

core/kotlinx-coroutines-core/test/guide/example-channel-06.kt

+9-7
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,15 @@ package kotlinx.coroutines.guide.channel06
88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010

11+
fun main() = runBlocking<Unit> {
12+
//sampleStart
13+
val producer = produceNumbers()
14+
repeat(5) { launchProcessor(it, producer) }
15+
delay(950)
16+
producer.cancel() // cancel producer coroutine and thus kill them all
17+
//sampleEnd
18+
}
19+
1120
fun CoroutineScope.produceNumbers() = produce<Int> {
1221
var x = 1 // start from 1
1322
while (true) {
@@ -21,10 +30,3 @@ fun CoroutineScope.launchProcessor(id: Int, channel: ReceiveChannel<Int>) = laun
2130
println("Processor #$id received $msg")
2231
}
2332
}
24-
25-
fun main(args: Array<String>) = runBlocking<Unit> {
26-
val producer = produceNumbers()
27-
repeat(5) { launchProcessor(it, producer) }
28-
delay(950)
29-
producer.cancel() // cancel producer coroutine and thus kill them all
30-
}

core/kotlinx-coroutines-core/test/guide/example-channel-07.kt

+10-9
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,22 @@ package kotlinx.coroutines.guide.channel07
77

88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
10-
import kotlin.coroutines.*
1110

12-
suspend fun sendString(channel: SendChannel<String>, s: String, time: Long) {
13-
while (true) {
14-
delay(time)
15-
channel.send(s)
16-
}
17-
}
18-
19-
fun main(args: Array<String>) = runBlocking {
11+
fun main() = runBlocking {
12+
//sampleStart
2013
val channel = Channel<String>()
2114
launch { sendString(channel, "foo", 200L) }
2215
launch { sendString(channel, "BAR!", 500L) }
2316
repeat(6) { // receive first six
2417
println(channel.receive())
2518
}
2619
coroutineContext.cancelChildren() // cancel all children to let main finish
20+
//sampleEnd
21+
}
22+
23+
suspend fun sendString(channel: SendChannel<String>, s: String, time: Long) {
24+
while (true) {
25+
delay(time)
26+
channel.send(s)
27+
}
2728
}

core/kotlinx-coroutines-core/test/guide/example-channel-08.kt

+3-2
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ package kotlinx.coroutines.guide.channel08
77

88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
10-
import kotlin.coroutines.*
1110

12-
fun main(args: Array<String>) = runBlocking<Unit> {
11+
fun main() = runBlocking<Unit> {
12+
//sampleStart
1313
val channel = Channel<Int>(4) // create buffered channel
1414
val sender = launch { // launch sender coroutine
1515
repeat(10) {
@@ -20,4 +20,5 @@ fun main(args: Array<String>) = runBlocking<Unit> {
2020
// don't receive anything... just wait....
2121
delay(1000)
2222
sender.cancel() // cancel sender coroutine
23+
//sampleEnd
2324
}

0 commit comments

Comments
 (0)