微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

SwitchIfEmpty链导致StackOverflowError

如何解决SwitchIfEmpty链导致StackOverflowError

以下代码导致StackOverflowError。编写这样的代码时应该发生这种情况吗?还是在框架中需要解决

代码已经过rxjava-2.2.19的测试。来源here

样本

下面的代码基于我们用来完成某种带有中断条件的循环的代码(请参见flowable = flowable.switchIfEmpty(third(s)))。显然,这种方法效果不佳。

public class SwitchIfEmptyTest {

    public static void main(String[] args) {
        SwitchIfEmptyDemo demo = new SwitchIfEmptyDemo();
        demo.one("foo")
                .blockingForEach(s -> System.out.println(s));
    }

    static class SwitchIfEmptyDemo {

        private SomeSource source = new SomeSource();

        public Flowable<String> one(String input) {
            return Flowable.<String>empty()
                    .switchIfEmpty(two(input));
        }

        public Flowable<String> two(String input) {
            return Flowable.<String>create(emitter -> {
                emitter.onNext(input);
                emitter.onComplete();
            },BackpressureStrategy.ERROR)
                    .flatMap(inputFlowable -> {
                        return source.read()
                                .toList()
                                .toFlowable()
                                .flatMap(strings -> {
                                    Flowable<String> flowable = Flowable.empty();
                                    for (String s : strings) {
                                        flowable = flowable.switchIfEmpty(third(s));
                                    }
                                    return flowable;
                                });
                    });
        }

        public Flowable<String> third(String input) {
            //System.out.println("Value " + input);
            return Flowable.empty();
        }
    }

    static class SomeSource {

        public Flowable<String> read() {
            return Flowable.create(emitter -> {
                for (int i = 0; i < 1_000_000; i++) {
                    emitter.onNext("Some values " + i);
                }
                emitter.onComplete();
            },BackpressureStrategy.ERROR);
        }
    }
}

Stacktrace

Exception in thread "main" java.lang.StackOverflowError
at java.lang.classLoader.defineClass1(Native Method)
at java.lang.classLoader.defineClass(ClassLoader.java:756)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.urlclassloader.defineClass(urlclassloader.java:468)
at java.net.urlclassloader.access$100(urlclassloader.java:74)
at java.net.urlclassloader$1.run(urlclassloader.java:369)
at java.net.urlclassloader$1.run(urlclassloader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.urlclassloader.findClass(urlclassloader.java:362)
at java.lang.classLoader.loadClass(ClassLoader.java:418)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.classLoader.loadClass(ClassLoader.java:351)
at io.reactivex.Flowable.subscribe(Flowable.java:14939)
at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty.subscribeActual(FlowableSwitchIfEmpty.java:32)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty.subscribeActual(FlowableSwitchIfEmpty.java:32)
at io.reactivex.Flowable.subscribe(Flowable.java:14935)
at io.reactivex.internal.operators.flowable.FlowableSwitchIfEmpty.subscribeActual(FlowableSwitchIfEmpty.java:32)
...

解决方法

为了摆脱StackOverflowError,一种可能性是使用takeWhile运算符(请参阅full diff)。

public Flowable<String> two(String input) {
    return Flowable.<String>create(emitter -> {
        emitter.onNext(input);
        emitter.onComplete();
    },BackpressureStrategy.ERROR)
            .flatMap(inputFlowable -> {
                return source.read()
                        .flatMap(this::third)
                        .takeWhile(s -> s.equals("false"));
            });
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。