I have created a maven project in Intellij IDEA , I'm trying to execute the below simple rxjava code
Observable.just(1,2,3,4)
.observeOn(Schedulers.io())
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println(integer);
}
});
I expect the result 1 , 2 , 3 , 4
to be printed in the io
thread. But when I run the code, it doesn't print anything.
If I remove the observeOn(Schedulers.io)
, then it prints as expected in the main thread.
I created creating a custom Thread pool as shown below
Executor executor = Executors.newFixedThreadPool(1);
Observable.just(1,2,3,4)
.observeOn(Schedulers.from(executor))
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
System.out.println(integer);
}
});
This is working properly. The Schedulers.newThread()
and Schedulers.computation()
threads also working properly.
Only Schedulers.io
has no effect in the code. Why is that?
Below is my dependency for Rxjava
<dependency>
<groupId>io.reactivex.rxjava2</groupId>
<artifactId>rxjava</artifactId>
<version>2.2.4</version>
</dependency>