OverflowStrategy是否未考虑?

问题描述 投票:0回答:1

我创建了一个Flux,该Flux不断追求新的整数值。我有一个在其自己的线程(.publishOn(single()))中运行的订阅服务器。无论我使用哪种策略(最新或其他策略),我总是得到相同的结果:

*** Received 1 with thread single-1
>>> Generated 1 with thread main
>>> Generated 2 with thread main
>>> Generated 3 with thread main
>>> Generated 4 with thread main
>>> Generated 5 with thread main
>>> Generated 6 with thread main
>>> Generated 7 with thread main
>>> Generated 8 with thread main
>>> Generated 9 with thread main
*** Received 2 with thread single-1
*** Received 3 with thread single-1
*** Received 4 with thread single-1
*** Received 5 with thread single-1
*** Received 6 with thread single-1
*** Received 7 with thread single-1

根据我的理解,通过设置latest,我应该错过somme整数no?

Flux<Integer> IntGenerator = Flux.create(e -> {
            AtomicInteger iteration = new AtomicInteger(1);
            while (iteration.intValue() < 10) {
                int value = iteration.getAndIncrement();
                e.next(value);
                if (value < 10) {
                    System.out.println(">>> Generated " + value + " with thread " + Thread.currentThread().getName());
                }
            }
        }, FluxSink.OverflowStrategy.LATEST);

        IntGenerator.publishOn(single())
                .subscribe(new Subscriber<Integer>() {
                    private Subscription s;

                    @Override
                    public void onSubscribe(final Subscription subscription) {
                        System.out.println("Subscribed");
                        s = subscription;
                        s.request(1);
                    }

                    @Override
                    public void onNext(final Integer integer) {
                        System.out.println("*** Received " + integer + " with thread " + Thread.currentThread().getName());
                        try {
                            Thread.sleep(10000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        s.request(1);
                    }

                    @Override
                    public void onError(final Throwable throwable) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });

        latch.await(120L, TimeUnit.SECONDS);
java reactive reactor
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.