Sun. Dec 8th, 2019

Host your Website

Read and learn

FatCow Web Hosting $1.00/mo* Trust your web hosting to the #1 web host provider, GoDaddy!

Java Streams vs Reactive Streams: Which, When, How, and Why? by Venkat Subramaniam

1 min read



Java 8 introduced Streams and Java 9 now has Reactive API. Which one should we choose, when should we choose them, why, and how to use one vs. the other. This session will answer all those questions, by diving deep into the APIs using live code examples.

Venkat Subramaniam
From Agile Developer, Inc.
Dr. Venkat Subramaniam is an award-winning author, founder of Agile Developer, Inc., creator of agilelearner.com, and an instructional professor at the University of Houston.

He has trained and mentored thousands of software developers in the US, Canada, Europe, and Asia, and is a regularly-invited speaker at several international conferences. Venkat helps his clients effectively apply and succeed with sustainable agile practices on their software projects.

Venkat is a (co)author of multiple technical books, including the 2007 Jolt Productivity award winning book Practices of an Agile Developer. You can find a list of his books at agiledeveloper.com.

20 thoughts on “Java Streams vs Reactive Streams: Which, When, How, and Why? by Venkat Subramaniam

  1. Created a short summary for my own use:

    In the context of imperative programming:

    1. List-based functional programming is eagerly evaluated.
    2. Stream() introduced in Java 8 and is the seam showing we're not programming imperatively anymore. Is it required, though?
    a. imperative – what and how
    b. declarative – what and not how + higher order functions = FP. Code reads like the problem statement, through "internal iteration"/ "iteration on autopilot"
    Mutability is OK, but shared mutability is not.
    3. In OOP we encapsulate moving parts – in FP, we eliminate them.
    4. Is stream API slow – you don't want a faster bicycle, but a rocket – EASIER TO PARALLELIZE
    5. List is bucket, Stream is pipeline. You don't store water in a pipeline, you just move it through there.
    6. Functional composition (builder pattern) is essential.
    7. Lazy evaluation example – removing the terminal (collect) method – functions not being called anymore, as result not needed.
    8. Many languages give programming pattern, such as Groovy, but laziness not there, as it depends on implementation.

    Limitations of streams:
    1. Single use only. Example where a stream is assigned to a variable, printed and then used with different filter condition. Result: IllegalStateException: stream has been operated on or closed.
    a. Concept: streams like QTips – use once and throw away.
    2. Not possible to fork in the middle. Only a single terminal operation.
    3. How do you deal with exceptions: sorry :(. Exception handling is an imperative-style idea: call a function, which calls a function, etc. and destroys your callstack.
    a. Scala has Mono monads and Either objects – provides safe handling, which is propagated and used in downstream stages. Lacks cohesion!
    b. Still good luck – we want pure functions and exceptions normally come from impurity.

    Reactive systems:

    1. Came out of Microsoft research, but just a new name for old concept – system that reacts to stimuli
    2. Four pillars of reactive programming:
    a. Elasticity
    b. Message-driven
    c. Responsive
    d. Resilience – circuit breakers built in, partial access to application
    3. Close to the 80's concept of dataflow computing: d → f → d →f
    a. Instructions ready to fire when the data is available
    b. Serverless computing (AWS Lambda) == dataflow programming. At the minute computation is ready and prepared it can run on ANY server with predefined affinity
    4. Reactive programming is FP++ – builds on lazy evaluation and functional composition
    a. Example with Flowable and subscribe on it
    5. Both Java Streams and Reactive ones push data, regular iterators pull. They are similar to Observable, you register to it and it pushes data to you.
    6. Both can return 0, 1 or more data.
    7. Java 8 Streams deal only with data. RS contain three channels (Data, Err, Complete). In RS error is just another form of data.
    8. RS are asynchronous, so you are not locked into parallelism (sync).
    9. RS can handle consumer back-pressure.
    10. Multiple subscribers in RS, where single pipeline in Java Streams
    11. Interface differences:
    a. Reactive Stream:
    – Publisher
    – Subscriber
    – Subscription – session between emitter and subscriber, can hold context
    – Processor – publisher + subscriber
    – Implementations: RxJava, Akka, Reactor (Spring 5), RSocket
    b. Java reactive streams – same interface, since Java 9 – in the java.util.concurrent.Flow.* class

  2. Thanks again for providing inspiring and great talk of the Reactive Streams concept. Hats off to your relentless contribution to the Java community. You are a truly Inspiring tech talker.

  3. @37 Exceptions. You can be proactive by using a filter to eliminate problematic data from stream. This works for many kinds of cases, e.g. if the list of numbers in the example contained a null.

Comments are closed.

Copyright © All rights reserved. | Newsphere by AF themes.