Skip to content

Commit 6644767

Browse files
committed
Change example-reactive-basic-06 to the main thread to make it more predictable
1 parent 6231188 commit 6644767

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

reactive/coroutines-guide-reactive.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ flowable that sends five integers from 1 to 5.
370370
It prints a message to the output before invocation of
371371
suspending [send][SendChannel.send] function, so that we can study how it operates.
372372

373-
The integers are generated in [CommonPool], but subscription is shifted
373+
The integers are generated in the context of the main thread, but subscription is shifted
374374
to another thread using Rx
375375
[observeOn](http://reactivex.io/RxJava/2.x/javadoc/io/reactivex/Flowable.html#observeOn(io.reactivex.Scheduler,%20boolean,%20int))
376376
operator with a buffer of size 1.
@@ -383,9 +383,9 @@ import io.reactivex.schedulers.Schedulers
383383
-->
384384

385385
```kotlin
386-
fun main(args: Array<String>) {
387-
// coroutine -- fast producer of elements in common pool
388-
val source = rxFlowable(CommonPool) {
386+
fun main(args: Array<String>) = runBlocking<Unit> {
387+
// coroutine -- fast producer of elements in the context of the main thread
388+
val source = rxFlowable(context) {
389389
for (x in 1..5) {
390390
println("Sending $x ...")
391391
send(x) // this is a suspending function
@@ -399,7 +399,7 @@ fun main(args: Array<String>) {
399399
println("Received $x")
400400
Thread.sleep(200) // 200 ms to process each item
401401
}
402-
Thread.sleep(2000) // hold on main thread for couple of seconds
402+
delay(2000) // suspend main thread for couple of seconds
403403
}
404404
```
405405

@@ -940,9 +940,9 @@ coroutines for complex pipelines with fan-in and fan-out between multiple worker
940940
<!--- DOCS_ROOT kotlinx-coroutines-core/target/dokka/kotlinx-coroutines-core -->
941941
<!--- INDEX kotlinx.coroutines.experimental -->
942942
[runBlocking]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/run-blocking.html
943-
[CommonPool]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-common-pool/index.html
944943
[launch]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/launch.html
945944
[CoroutineScope.context]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-coroutine-scope/context.html
945+
[CommonPool]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-common-pool/index.html
946946
[Unconfined]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-unconfined/index.html
947947
[Job.join]: https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.experimental/-job/join.html
948948
<!--- INDEX kotlinx.coroutines.experimental.channels -->

reactive/kotlinx-coroutines-rx2/src/test/kotlin/guide/example-reactive-basic-06.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ import kotlinx.coroutines.experimental.*
2121
import kotlinx.coroutines.experimental.rx2.rxFlowable
2222
import io.reactivex.schedulers.Schedulers
2323

24-
fun main(args: Array<String>) {
25-
// coroutine -- fast producer of elements in common pool
26-
val source = rxFlowable(CommonPool) {
24+
fun main(args: Array<String>) = runBlocking<Unit> {
25+
// coroutine -- fast producer of elements in the context of the main thread
26+
val source = rxFlowable(context) {
2727
for (x in 1..5) {
2828
println("Sending $x ...")
2929
send(x) // this is a suspending function
@@ -37,5 +37,5 @@ fun main(args: Array<String>) {
3737
println("Received $x")
3838
Thread.sleep(200) // 200 ms to process each item
3939
}
40-
Thread.sleep(2000) // hold on main thread for couple of seconds
40+
delay(2000) // suspend main thread for couple of seconds
4141
}

0 commit comments

Comments
 (0)