1 - About

ReactiveX is Functional reactive programming library (implemented in several languages)

It composes asynchronous and event-based programs using observable asynchronous sequences of multiple items.

It's based on reactive stream.

ReactiveX is a combination of:

ReactiveX operates on discrete values that are emitted over time

also known as LINQ to Events

The goal of Rx is to coordinate and orchestrate :

  • event-based - low latency - (event-based app)
  • and asynchronous - high-latency - computations (Web sockets, Web services, ..)

3 - Use case

  • run some computation, network request on a background thread and show the results (or error) on the UI thread.

4 - Visualisation

Marble diagrams are interactive diagrams that depict how Rx operators work with observable sequences.

5 - DataFlow

The dataflows consist of:

  • a source,
  • zero or more intermediate steps
  • followed by a data consumer or combinator step (where the step is responsible to consume the dataflow by some means)

The object traveling along the dataflow is known as emission, emits, item, event, signal, data and message represent .

5.1 - Main flow

    .operator2()    // ↑↑↑ upstream   ↑↑↑
    .operator3()    // ↓↓↓ downstream ↓↓↓

5.2 - Sub flow

  .flatMap(value -> source


Flowable<Inventory> inventorySource = warehouse.getInventoryAsync();
    .flatMap(inventoryItem -> 
            .map(demand -> "Item " + inventoryItem.getName() + " has demand " + demand)

6 - Model

6.1 - Observable

You can think of the Observable class as a “push” equivalent to Iterable, which is a “pull.” With an Iterable, the consumer pulls values from the producer and the thread blocks until those values arrive. By contrast, with an Observable the producer pushes values to the consumer whenever values are available. This approach is more flexible, because values can arrive synchronously or asynchronously.

Event Iterable (Pull) Observable (Push)
retrieve data T next() onNext(T)
discover error throws Exception onError(Exception)
complete returns onCompleted()

note: a BlockingObservable behaves very much like a Iterable .


  • Iterable

  .map({ s -> return s + " transformed" })
  .forEach({ println "next => " + it })

  • Observable

  .map({ s -> return s + " transformed" })
  .subscribe({ println "onNext => " + it })

The contract of a Observable is that:

  • zero to N data events can happen,
  • terminated by:
    • a complete event
    • or an error

The ReactiveX Observable model:

single items multiple items
synchronous T getData() Iterable<T> getData()
asynchronous Future<T> getData() Observable<T> getData()

where: Asynchronous - Future

6.2 - Operator

ReactiveX allows you to chain operators together to transform and compose Observables. This style of chaining methods is called a fluent API.

RxJava's reactive types are immutable; each of the method calls returns a new Flowable with added behavior.

7 - Data Flow State

7.1 - Assembly time

assembly time is the preparation of dataflows by applying various intermediate operators

With the below code, the data is not flowing yet

Flowable<Integer> flow = Flowable.range(1, 5)
.map(v -> v * v)
.filter(v -> v % 3 == 0)

7.2 - Subscription time

This is a temporary state when subscribe() is called on a flow that establishes the chain of processing steps internally:


This is when the subscription side-effects are triggered (see doOnSubscribe). Some sources block or start emitting items right away in this state.

7.3 - Runtime

This is the state when the flows are actively emitting items, errors or completion signals

Observable.create(emitter -> {
     while (!emitter.isDisposed()) {
         long time = System.currentTimeMillis();
         if (time % 2 != 0) {
             emitter.onError(new IllegalStateException("Odd millisecond!"));
.subscribe(System.out::println, Throwable::printStackTrace);

Practically, this is when the body of the given example above executes.

8 - Flows

RxJava 3 features several base classes (flow) where you can discover operators.

8..1 - 0..N flows

8..2 - 0..1 flows

1 item or no item flow

8.1 - Parallel

Flows are sequential in nature split into processing stages that may run concurrently with each other.

It means running independent flows and merging their results back into a single flow.


Flowable.range(1, 10)
  .flatMap(v ->Flowable.just(v)
        .map(w -> w * w)

where the operator flatMap

  • maps each number from 1 to 10 into its own individual Flowable
  • runs them
  • and merges the computed squares back into the main flow


  • flatMap doesn't guarantee any order and the items from the inner flows may end up interleaved.
  • concatMap that maps and runs one inner flow at a time
  • concatMapEager which runs all inner flows “at once” but the output flow will be in the order those inner flows were created.

Parallel flows may be initiated also with the Flowable Parallel

Flowable.range(1, 10)
                .map(v -> v * v)

9 - Usage

To use RxJava, you

  • create Observables (which emit data items), Doc - Creating-Observables
  • transform those Observables with Observable operators (sort of functional programming method)
  • and then observe and react to these sequences of interesting items (by:
    • implementing Observers or Subscribers
    • and then subscribing them to the resulting transformed Observables).

9.1 - Creating Observable

It extends the observer pattern to:

  • support sequences of data and/or events
  • and adds operators

Observables will:

  • synchronously invoke the onNext( ) method of any subscriber that subscribes to them, for each item to be emitted by the Observable,
  • will then invoke the subscriber’s onCompleted( ) method.

You can implement asynchronous i/o, computational operations, or even infinite streams of data by designing your own Observable and implementing it with the create( ) method.

9.2 - Transforming Observables with Operators

9.3 - Error Handling

9.4 - High Latency Computation

  • Typically, you can move computations or blocking IO to some other thread via subscribeOn.
  • Once the data is ready, you can make sure they get processed on the foreground or GUI thread via observeOn.

Flowable.fromCallable(() -> {
    Thread.sleep(1000); //  imitate expensive computation
    return "Done";
  .subscribe(System.out::println, Throwable::printStackTrace);

Thread.sleep(2000); // <--- wait for the flow to finish

10 - Concept

10.1 - Schedulers

A scheduler is a thread abstraction (a uniform concurrency API)

ReactiveX operators work with Schedulers (not directly with Threads or ExecutorServices)

The below standard schedulers are accessible via the Schedulers utility class.

  • Schedulers.computation(): Run computation intensive work on a fixed number of dedicated threads in the background. Most asynchronous operators use this as their default Scheduler.
  • Run I/O-like or blocking operations on a dynamically changing set of threads.
  • Schedulers.single(): Run work on a single thread in a sequential and FIFO manner.
  • Schedulers.trampoline(): Run work in a sequential and FIFO manner in one of the participating threads, usually for testing purposes.
  • Schedulers.from(Executor): wrap an existing Executor (and its subtypes such as ExecutorService) into a Scheduler to have a larger but still fixed pool of threads (unlike computation() and io() respectively).
  • Only on Android: AndroidSchedulers.mainThread(),
  • Only on Swing: SwingScheduler.instance()
  • Only on JavaFx: JavaFXSchedulers.gui().

In RxJava the default Schedulers run on daemon threads, which means:

  • once the Java main thread exits,
  • they all get stopped and background computations may never happen.

Sleeping for some time (Thread.sleep(2000)) lets you see the output of the flow on the console.

11 - Documentation / Reference

Data Science
Data Analysis
Data Science
Linear Algebra Mathematics

Powered by ComboStrap