I'm learning how the scheduler works. So I tried to set the number to 100. with publishOn it works (it sets number to 100), but with subscribeOn it doesn't set number to 100. I don't understand why?
@Test
fun reactor01_LearnSchedulers(){
var number = 0
Mono.just(100)
.doOnNext { numb -> number = numb }
// .subscribeOn(Schedulers.boundedElastic()) this gives result: 0
.publishOn(Schedulers.boundedElastic()) // this gives result: 100
.subscribe()
println("Result : $number")
}
update: After I added some code I found that the results are not consistent.
fun reactor01_LearnSchedulers(){
var number = 0
Mono.just(100)
.doOnNext { numb -> number = numb }
.doOnNext { println("Result from inside: $number") }
.publishOn(Schedulers.boundedElastic())
.subscribe()
println("Result from outside: $number")
// with publishOn Result from inside: 100, Result from outside: 100
// with subscribeOn Result from outside: 0, Result from inside: 100
}