How to subscribe several subscribers to Observable or Flowable?

白昼怎懂夜的黑 提交于 2019-12-02 14:05:04

问题


In Hello World example there is one subscriber

   public static void main(String[] args) {
      Flowable.just("Hello world").subscribe(System.out::println);
   }

How to make two or more?


回答1:


You can subscribe multiple subsctibers to any observable/flowable. Just repeat subscribe call as many times as you need.

Flowable<String> source = Flowable.just("Hello world");
source.subscribe(System.out::println);
source.subscribe(System.out::println);
...

There is difference in hot and cold observables in the way they handle such multiple subscriptions.

Cold observables/flowables re-request items from source for every new subscriber. For example, Flowable.fromCallable(c) will invoke c every time it is subscribed to.

Hot observables/flowables share same source subscription with all subscribers, i.e. they do not request new items from source for every new subscriber. New items get propagated to all currently subscribed subscribers.



来源:https://stackoverflow.com/questions/41824177/how-to-subscribe-several-subscribers-to-observable-or-flowable

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!