RxJava vs Java 8 Parallelism Stream

后端 未结 3 568
春和景丽
春和景丽 2021-02-01 04:33

What are all the similarities and diferences between them, It looks like Java Parallel Stream has some of the element available in RXJava, is that right?

相关标签:
3条回答
  • 2021-02-01 04:46

    Stream is pull based. Personally I feel it is Oracle's answer to C# IEnumerable<>, LINQ and their related extension methods.

    RxJava is push based, which I am not sure whether it is .NET's reactive extensions released first or Rx project goes live first.

    Conceptually they are totally different and their applications are also different.

    If you are implementing a text searching program on a text file that's so large that you can't load everything and fit into memory, you would probably want to use Stream since you can easily determine if you have next lines available by keeping track of your iterator, and scan line by line.

    Another application of Stream would be parallel calculations on a collection of data. Nowadays every machine has multiple cores but you won't know easily exactly how many cores your client machine are available. It would be hard to pre-configure the number of threads to operate. So we use parallel stream and let the JVM to determine that for us (supposed to be more optimal).

    On the other hand, if you are implementing a program that takes an user input string and searches for available videos on the web, you would use RX since you won't even know when the program will start getting any results (or receive an error of network timeout). To make your program responsive you have to let the program "subscribe" for network updates and complete signals.

    Another common application of Rx is on GUI to "detect user finished input" without requiring the user to click a button to confirm. For example you want to have a text field whenever the user stops typing you start searching without waiting a "Search button" click. In this case you use Rx to create an observable on "KeyEvent" and "throttle" (e.g. at 500ms), so that whenever he stopped typing for 500ms you receive an onNext() to "start searching".

    0 讨论(0)
  • 2021-02-01 04:53

    Rx is an API for creating and processing observable sequences. The Streams API is for processing iterable sequences. Rx sequences are push-based; you are notified when an element is available. A Stream is pull-based; it "asks" for items to process. They may appear similar because they both support similar operators/transforms, but the mechanics are essentially opposites of each other.

    0 讨论(0)
  • 2021-02-01 04:53

    There is also a difference in threading.

    Stream#parallel splits the sequence into parts, and each part is processed in the separate thread.

    Observable#subscribeOn and Observable#observeOn are both 'move' execution to another thread, but don't split the sequence.

    In other words, for any particular processing stage:

    • parallel Stream may process different elements on different threads
    • Observable will use one thread for the stage

    E. g. we have Observable/Stream of many elements and two processing stages:

    Observable.create(...)
        .observeOn(Schedulers.io())
        .map(x -> stage1(x))
        .observeOn(Schedulers.io())
        .map(y -> stage2(y))
        .forEach(...);
    
    Stream.generate(...)
        .parallel()
        .map(x -> stage1(x))
        .map(y -> stage2(y))
        .forEach(...);
    

    Observable will use no more than 2 additional threads (one per stage), so no two x'es or y's are accessed by different threads. Stream, on the countrary, may span each stage across several threads.

    0 讨论(0)
提交回复
热议问题