Skip to content

Commit 0eee3c3

Browse files
AlexanderPrendotaelizarov
authored andcommitted
docs: make select-expression & shared state and concurrency runnable
1 parent e0b7b15 commit 0eee3c3

15 files changed

+362
-86
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ package kotlinx.coroutines.guide.select01
88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010
import kotlinx.coroutines.selects.*
11-
import kotlinx.coroutines.*
12-
import kotlin.coroutines.*
1311

1412
fun CoroutineScope.fizz() = produce<String> {
1513
while (true) { // sends "Fizz" every 300 ms
@@ -37,10 +35,12 @@ suspend fun selectFizzBuzz(fizz: ReceiveChannel<String>, buzz: ReceiveChannel<St
3735
}
3836

3937
fun main() = runBlocking<Unit> {
38+
//sampleStart
4039
val fizz = fizz()
4140
val buzz = buzz()
4241
repeat(7) {
4342
selectFizzBuzz(fizz, buzz)
4443
}
45-
coroutineContext.cancelChildren() // cancel fizz & buzz coroutines
44+
coroutineContext.cancelChildren() // cancel fizz & buzz coroutines
45+
//sampleEnd
4646
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.select02
88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010
import kotlinx.coroutines.selects.*
11-
import kotlin.coroutines.*
1211

1312
suspend fun selectAorB(a: ReceiveChannel<String>, b: ReceiveChannel<String>): String =
1413
select<String> {
@@ -25,8 +24,9 @@ suspend fun selectAorB(a: ReceiveChannel<String>, b: ReceiveChannel<String>): St
2524
"b -> '$value'"
2625
}
2726
}
28-
27+
2928
fun main() = runBlocking<Unit> {
29+
//sampleStart
3030
val a = produce<String> {
3131
repeat(4) { send("Hello $it") }
3232
}
@@ -36,5 +36,6 @@ fun main() = runBlocking<Unit> {
3636
repeat(8) { // print first eight results
3737
println(selectAorB(a, b))
3838
}
39-
coroutineContext.cancelChildren()
40-
}
39+
coroutineContext.cancelChildren()
40+
//sampleEnd
41+
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.select03
88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010
import kotlinx.coroutines.selects.*
11-
import kotlin.coroutines.*
1211

1312
fun CoroutineScope.produceNumbers(side: SendChannel<Int>) = produce<Int> {
1413
for (num in 1..10) { // produce 10 numbers from 1 to 10
@@ -21,6 +20,7 @@ fun CoroutineScope.produceNumbers(side: SendChannel<Int>) = produce<Int> {
2120
}
2221

2322
fun main() = runBlocking<Unit> {
23+
//sampleStart
2424
val side = Channel<Int>() // allocate side channel
2525
launch { // this is a very fast consumer for the side channel
2626
side.consumeEach { println("Side channel has $it") }
@@ -30,5 +30,6 @@ fun main() = runBlocking<Unit> {
3030
delay(250) // let us digest the consumed number properly, do not hurry
3131
}
3232
println("Done consuming")
33-
coroutineContext.cancelChildren()
33+
coroutineContext.cancelChildren()
34+
//sampleEnd
3435
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66
package kotlinx.coroutines.guide.select04
77

88
import kotlinx.coroutines.*
9-
import kotlinx.coroutines.channels.*
109
import kotlinx.coroutines.selects.*
1110
import java.util.*
12-
11+
1312
fun CoroutineScope.asyncString(time: Int) = async {
1413
delay(time.toLong())
1514
"Waited for $time ms"
@@ -21,6 +20,7 @@ fun CoroutineScope.asyncStringsList(): List<Deferred<String>> {
2120
}
2221

2322
fun main() = runBlocking<Unit> {
23+
//sampleStart
2424
val list = asyncStringsList()
2525
val result = select<String> {
2626
list.withIndex().forEach { (index, deferred) ->
@@ -32,4 +32,5 @@ fun main() = runBlocking<Unit> {
3232
println(result)
3333
val countActive = list.count { it.isActive }
3434
println("$countActive coroutines are still active")
35+
//sampleEnd
3536
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ package kotlinx.coroutines.guide.select05
88
import kotlinx.coroutines.*
99
import kotlinx.coroutines.channels.*
1010
import kotlinx.coroutines.selects.*
11-
import kotlin.coroutines.*
12-
11+
1312
fun CoroutineScope.switchMapDeferreds(input: ReceiveChannel<Deferred<String>>) = produce<String> {
1413
var current = input.receive() // start with first received deferred value
1514
while (isActive) { // loop while not cancelled/closed
@@ -37,6 +36,7 @@ fun CoroutineScope.asyncString(str: String, time: Long) = async {
3736
}
3837

3938
fun main() = runBlocking<Unit> {
39+
//sampleStart
4040
val chan = Channel<Deferred<String>>() // the channel for test
4141
launch { // launch printing coroutine
4242
for (s in switchMapDeferreds(chan))
@@ -52,4 +52,5 @@ fun main() = runBlocking<Unit> {
5252
delay(1000) // give it time to process
5353
chan.close() // close the channel ...
5454
delay(500) // and wait some time to let it finish
55+
//sampleEnd
5556
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
package kotlinx.coroutines.guide.sync01
77

88
import kotlinx.coroutines.*
9-
import kotlin.system.*
10-
import kotlin.coroutines.*
9+
import kotlin.system.*
1110

1211
suspend fun CoroutineScope.massiveRun(action: suspend () -> Unit) {
1312
val n = 100 // number of coroutines to launch
@@ -26,8 +25,10 @@ suspend fun CoroutineScope.massiveRun(action: suspend () -> Unit) {
2625
var counter = 0
2726

2827
fun main() = runBlocking<Unit> {
28+
//sampleStart
2929
GlobalScope.massiveRun {
3030
counter++
3131
}
3232
println("Counter = $counter")
33+
//sampleEnd
3334
}

core/kotlinx-coroutines-core/test/guide/example-sync-01b.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package kotlinx.coroutines.guide.sync01b
77

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

1211
suspend fun CoroutineScope.massiveRun(action: suspend () -> Unit) {
1312
val n = 100 // number of coroutines to launch
@@ -27,8 +26,10 @@ val mtContext = newFixedThreadPoolContext(2, "mtPool") // explicitly define cont
2726
var counter = 0
2827

2928
fun main() = runBlocking<Unit> {
29+
//sampleStart
3030
CoroutineScope(mtContext).massiveRun { // use it instead of Dispatchers.Default in this sample and below
3131
counter++
3232
}
3333
println("Counter = $counter")
34+
//sampleEnd
3435
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package kotlinx.coroutines.guide.sync02
77

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

1211
suspend fun CoroutineScope.massiveRun(action: suspend () -> Unit) {
1312
val n = 100 // number of coroutines to launch

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ package kotlinx.coroutines.guide.sync03
88
import kotlinx.coroutines.*
99
import java.util.concurrent.atomic.*
1010
import kotlin.system.*
11-
import kotlin.coroutines.*
1211

1312
suspend fun CoroutineScope.massiveRun(action: suspend () -> Unit) {
1413
val n = 100 // number of coroutines to launch
@@ -27,8 +26,10 @@ suspend fun CoroutineScope.massiveRun(action: suspend () -> Unit) {
2726
var counter = AtomicInteger()
2827

2928
fun main() = runBlocking<Unit> {
29+
//sampleStart
3030
GlobalScope.massiveRun {
3131
counter.incrementAndGet()
3232
}
3333
println("Counter = ${counter.get()}")
34+
//sampleEnd
3435
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ package kotlinx.coroutines.guide.sync04
77

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

1211
suspend fun CoroutineScope.massiveRun(action: suspend () -> Unit) {
1312
val n = 100 // number of coroutines to launch
@@ -27,10 +26,12 @@ val counterContext = newSingleThreadContext("CounterContext")
2726
var counter = 0
2827

2928
fun main() = runBlocking<Unit> {
29+
//sampleStart
3030
GlobalScope.massiveRun { // run each coroutine with DefaultDispathcer
3131
withContext(counterContext) { // but confine each increment to the single-threaded context
3232
counter++
3333
}
3434
}
3535
println("Counter = $counter")
36+
//sampleEnd
3637
}

0 commit comments

Comments
 (0)