This is my test code in Kotlin:
fun main() {
rxjava()
}
fun rxjava() {
val queuSubject = PublishSubject.create<String>()
queuSubject
.map { t ->
val a = t.toLong()
Thread.sleep(6000 / a)
println("map $a called ${Thread.currentThread().name} ")
a
}
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe({
println("thread in subscription ${Thread.currentThread().name}")
}, {
println("error ${it.message}")
})
for (i in 1..3) {
Thread {
queuSubject.onNext("$i")
}.start()
}
Thread.sleep(15000)
}
I'm trying to run map
block and subscribe's onNext
block in different IO threads. But the output is like this:
map 3 called Thread-2
thread in subscription RxCachedThreadScheduler-2
map 2 called Thread-1
thread in subscription RxCachedThreadScheduler-2
map 1 called Thread-0
thread in subscription RxCachedThreadScheduler-2
As you can see It seems that calling subscribeOn
has no effect on PublishSubject's
stream and thread-0,thread-1 and thread-2
refers to the threads that call onNext
methods.
Additionally consider the code below:
fun main() {
rxjava()
}
fun rxjava() {
val queuSubject = PublishSubject.create<String>()
queuSubject
.map { t ->
val a = t.toLong()
Thread.sleep(6000 / a)
println("map $a called ${Thread.currentThread().name} ")
a
}
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe({
println("thread in subscription ${Thread.currentThread().name}")
}, {
println("error ${it.message}")
})
queuSubject.onNext("1")
queuSubject.onNext("2")
queuSubject.onNext("3")
Thread.sleep(15000)
}
I wrote the code above and saw that no output is printed. But If I remove subscribeOn
from the stream, messages are printed sequentially like the following:
map 1 called main
thread in subscription RxCachedThreadScheduler-1
map 2 called main
thread in subscription RxCachedThreadScheduler-1
map 3 called main
thread in subscription RxCachedThreadScheduler-1
What is the problem of these codes? Thanks.