Rxjava: Exception in thread "main" java.lang.StackOverflowError if interval create too many observable

Created on 27 Jun 2016  Â·  3Comments  Â·  Source: ReactiveX/RxJava

Observable.interval(1, TimeUnit.MILLISECONDS, Schedulers.immediate()).limit(5000)
            .subscribe(new Subscriber<Long>() {
                @Override
                public void onCompleted() {
                    System.out.println("------------------------");
                }

                @Override public void onError(Throwable e) {
                    System.out.println("error");
                }

                @Override public void onNext(Long aLong) {
                    System.out.println("=========== " + aLong + " ===========");
                }
});

Only occurs at "Schedulers.immediate()"
version: 1.0.10 and 1.1.6

Question

Most helpful comment

When you use the default scheduler (Schedulers.computation()) the observable emits on another thread. If your program exits just after the subscribe then the observable is not given a chance to run. Put in a long sleep just after the subscribe() call and you will see it working.

The immediate() scheduler is not safe for recursive scheduling on the current thread, use trampoline() instead.

All 3 comments

When you use the default scheduler (Schedulers.computation()) the observable emits on another thread. If your program exits just after the subscribe then the observable is not given a chance to run. Put in a long sleep just after the subscribe() call and you will see it working.

The immediate() scheduler is not safe for recursive scheduling on the current thread, use trampoline() instead.

Thanks for your suggest, it works

Was this page helpful?
0 / 5 - 0 ratings

Related issues

paulblessing picture paulblessing  Â·  3Comments

dzharikhin picture dzharikhin  Â·  4Comments

midnight-wonderer picture midnight-wonderer  Â·  3Comments

dimsuz picture dimsuz  Â·  4Comments

dsvoronin picture dsvoronin  Â·  4Comments