Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Observable interval scheduling on Schedulers.trampoline cause a infinite loop #5365

Closed
sopping opened this issue May 25, 2017 · 5 comments
Closed

Comments

@sopping
Copy link

sopping commented May 25, 2017

This code cause a infinite loop when snippt-1 interval complete.

        System.out.println("start");
        // snippt-1
        Observable.interval(1, TimeUnit.SECONDS, Schedulers.trampoline()).take(10)
                .subscribe(aLong -> System.out.println(aLong + "; " + Thread.currentThread().getName()), Throwable::printStackTrace,
                        () -> System.out.println("complete1."));
        // snippt-2
        Observable.interval(1, TimeUnit.SECONDS, Schedulers.trampoline()).take(10)
                .subscribe(aLong -> System.out.println(aLong + "; " + Thread.currentThread().getName()), Throwable::printStackTrace,
                        () -> System.out.println("complete2."));

        Thread.sleep(30000);
        System.out.println("end");

This code will be fine

        System.out.println("start");

        Observable.interval(1, TimeUnit.SECONDS).take(10).subscribeOn(Schedulers.trampoline())
                .subscribe(aLong -> System.out.println(aLong + "; " + Thread.currentThread().getName()), Throwable::printStackTrace,
                        () -> System.out.println("complete1."));

        Observable.interval(1, TimeUnit.SECONDS).take(10).subscribeOn(Schedulers.trampoline())
                .subscribe(aLong -> System.out.println(aLong + "; " + Thread.currentThread().getName()), Throwable::printStackTrace,
                        () -> System.out.println("complete2."));

        Thread.sleep(30000);
        System.out.println("end");

Is something wrong with me?

@sopping sopping changed the title Observable/Flowable interval scheduling on Schedulers.trampoline cause a infinite loop Observable interval scheduling on Schedulers.trampoline cause a infinite loop May 25, 2017
@akarnokd akarnokd added the 2.x label May 25, 2017
@akarnokd
Copy link
Member

It is the effect of sleeping-blocking nature of the trampoline scheduler that when it enters a synchronous periodic loop, the parent stream simply won't get the control to cancel that loop.

The second set of cases work because using subscribeOn has no effect (and runs on the main thread anyway) as the interval itself executes on the computation thread afterwards.

@sopping
Copy link
Author

sopping commented May 25, 2017

The tampoline works when I use 1.x.
I want to execute some interval sequentially on current thread. Can I?

@akarnokd
Copy link
Member

The tampoline works when I use 1.x.

API changes and scheduler use has changed that exposes this issue.

I want to execute some interval sequentially on current thread. Can I?

That is Thread.sleep() in a loop. Why do you want this?

I've posted a fix for this type of cases in #5367.

@sopping
Copy link
Author

sopping commented May 26, 2017

Is there some replaceable way to do the same thing now?
This code seems ok:

Observable.range(1, interval).subscribeOn(Schedulers.trampoline()).map(aLong -> {
            TimeUnit.SECONDS.sleep(1);
            return aLong;
        }).subscribe(aLong -> System.out.println(aLong + "; " + Thread.currentThread().getName()));

@akarnokd
Copy link
Member

akarnokd commented Jun 1, 2017

You don't try to stop that setup, plus the sleep is so small I guess the whole thing completes pretty quickly anyway.

Closing via #5367.

@akarnokd akarnokd closed this as completed Jun 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants