fangzhzh
07/03/2019, 11:53 PMfun printCurrentThread(tag: String) = println("$tag: ${Thread.currentThread().name.substringBefore("-")}")
fun main(args: Array<String>) {
printCurrentThread("main thread name")
val subject = BehaviorSubject.create(Unit)
subject
.subscribeOn(Schedulers.computation())
.subscribe {
printCurrentThread("subscribe onNext")
}
Thread.sleep(100L)
subject.onNext(Unit)
Thread.sleep(100L)
}
// The author said the code gives
main thread name: main
subscribe onNext: RxComputationScheduler
subscribe onNext: main
The author explains:
Correct answer is A because the default value is based on the subscribeOn (computation thread) and .onNext() of the subject is called from the main thread. Therefore the second emission will ignore the subscribeOn, change thread and will be emitted at the main thread.
But from my running, it gives
main thread name: main
subscribe onNext: main
my question is that is there really a Where the first RxComputationScheduler onNext()
?trevjones
07/04/2019, 1:04 AMval subject = BehaviorSubject.create(Unit)
is bad syntax, it was probably meant to be val subject = BehaviorSubject.create<Unit>()
which doesn’t put anything in it. meaning that if you subscribe before onNext is called there is nothing to deliver. if it had been val subject = BehaviorSubject.createDefault(Unit)
you would have seen
main thread name: main
subscribe onNext: computation*
subscribe onNext: main
fangzhzh
07/04/2019, 1:12 AM