Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
R

RxJava

> 前端框架
Open source

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

48.2K stars0 likes2 views
WebsiteGitHub

About

RxJava – Reactive Extensions for the JVM – a library for composing asynchronous and event-based programs using observable sequences for the Java VM.

RxJava: Reactive Extensions for the JVM

:information_source: 4.0.0 release: 2026.11.30. Monday. Track

RxJava is a Java VM implementation of Reactive Extensions: a library for composing asynchronous and event-based programs by using observable sequences.

It extends the observer pattern to support sequences of data/events and adds operators that allow you to compose sequences together declaratively while abstracting away concerns about things like low-level threading, synchronization, thread-safety and concurrent data structures.

Version 4.x (Javadoc)

  • :+1: Native Java 26 implementation1.
  • :+1: No 3rd party library required at runtime.
  • :+1: JPMS and :question: OSGi support still intact.
  • :+1: java.util.concurrent.Flow-based implementation.
  • :+1: Virtual Thread support; virtualCreate(), virtualTransform(), :eye: Schedulers.virtual().
  • :+1: New Streamable<T> built around Virtual Threads & virtual blocking. Think IAsyncEnumerable for Java. :satellite: in progress.
  • :+1: Using Java Cleaner API to detect resource leaks and using it for adaptive cleanups.
  • :information_source: Reactive Streams Test Compatibility Kit usage; Reactive-Streams.
  • :satellite: Reduce overload bloat by using record-based configurations.
  • :satellite: Internal optimizations now that I have the master :key:.
  • :eye: Possible usages for Scoped variables for context and per-item resource management.
  • :eye: Possible inclusion of 2nd and 3rd party operators.
  • :question: Android compatibility depends on your API level and what desugaring is available.
  • :warning: RxJava 3.x support will be toned down in the coming months, will be offered for +1 year after 4.x official release.

1 :stop: Unfortunately Java 27 has no language or API enhancements we can use, because Structured Concurrency remained preview for one.

Getting started

Setting up the dependency

The first step is to include RxJava 4 into your project, for example, as a Gradle compile dependency:

groovy
implementation "io.reactivex.rxjava4:rxjava:4.x.y"

(Please replace x and y with the latest version numbers: )

Hello World

The second is to write the Hello World program:

java
package rxjava.examples;

import io.reactivex.rxjava4.core.*;

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

Note that RxJava 4 components now live under io.reactivex.rxjava4 and the base classes and interfaces live under io.reactivex.rxjava4.core.

Base classes

RxJava 4 features several base classes you can discover operators on:

  • io.reactivex.rxjava4.core.Flowable: 0 .. N flows, supporting Reactive-Streams and backpressure,
  • io.reactivex.rxjava4.core.Observable: 0 .. N flows, no backpressure,
  • io.reactivex.rxjava4.core.Single: a flow of exactly 1 item or an error,
  • io.reactivex.rxjava4.core.Completable: a flow without items but only a completion or error signal,
  • io.reactivex.rxjava4.core.Maybe: a flow with no items, exactly one item or an error.
  • io.reactivex.rxjava4.core.Streamable: 0 .. N flows based on virtual blocking and CompletionStage-based state machines with native backpressure.

Some terminology

Upstream, downstream

The dataflows in RxJava 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):

java
source.operator1().operator2().operator3().subscribe(consumer);

source.flatMap(value -> source.operator1().operator2().operator3());

Here, if we imagine ourselves on operator2, looking to the left towards the source is called the upstream. Looking to the right towards the subscriber/consumer is called the downstream. This is often more apparent when each element is written on a separate line:

java
source
  .operator1()
  .operator2()
  .operator3()
  .subscribe(consumer)

Objects in motion

In RxJava's documentation, emission, emits, item, event, signal, data and message are considered synonyms and represent the object traveling along the dataflow.

Backpressure

When the dataflow runs through asynchronous steps, each step may perform different things with different speed. To avoid overwhelming such steps, which usually would manifest itself as increased memory usage due to temporary buffering or the need for skipping/dropping data, so-called backpressure is applied, which is a form of flow control where the steps can express how many items are they ready to process. This allows constraining the memory usage of the dataflows in situations where there is generally no way for a step to know how many items the upstream will send to it.

In RxJava, the dedicated Flowable class is designated to support backpressure and Observable is dedicated to the non-backpressured operations (short sequences, GUI interactions, etc.). The other types, Single, Maybe and Completable don't support backpressure nor should they; there is always room to store one item temporarily.

Since 4.0.0, the Streamable type gives natural backpressure because producers and consumers have to wait for each other to hand over data. Since waiting is blocking, the type natively works with Virtual Threaded ExecutorServices and the new Schedulers.virtual() Scheduler.

Assembly time

The preparation of dataflows by applying various intermediate operators happens in the so-called assembly time:

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

At this point, the data is not flowing yet and no side effects are happening.

Subscription time

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

java
flow.subscribe(System.out::println)

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

Runtime

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

java

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

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

Simple background computation

One of the common use cases for RxJava is to run some computation, network request on a background thread and show the results (or error) on the UI thread:

java
import io.reactivex.rxjava4.schedulers.Schedulers;

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

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

This style of chaining methods is called a fluent API which resembles the builder pattern. However, RxJava's reactive types are immutable; each of the method calls returns a new Flowable with added behavior. To illustrate, the example can be rewritten as follows:

java
Flowable<String> source = Flowable.fromCallable(() -> {
    Thread.sleep(1000); //  imitate expensive computation
    return "Done";
});

Flowable<String> runBackground = source.subscribeOn(Schedulers.io());

Flowable<String> showForeground = runBackground.observeOn(Schedulers.single());

showForeground.subscribe(System.out::println, Throwable::printStackTrace);

Thread.sleep(2000);

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.

Schedulers

RxJava operators don't work with Threads or ExecutorServices directly but with so-called Schedulers that abstract away sources of concurrency behind a uniform API. RxJava 4 features several standard schedulers accessible via 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.
  • Schedulers.cached(): Run I/O-like or blocking operations on a dynamically changing set of threads backed by native OS threads. :warning: Can exhaust system resources!
  • Schedulers.virtual(): Run I/O-like or blocking scatter-gather operations in a sequential manner on threads with virtualized stacks attached and detached to native OS threads on demand. :information_source: Helps with the issues around unboundedness of Schedulers.cached().
  • 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.createParallel(): Allows creating a Scheduler with an user-configurable worker pool size and other parameters to contrast computation() which is always set to availableProcessors()/configured amount globally.
  • Schedulers.createBlocking(): Allows creating an event-loop style Scheduler which runs tasks and blocks on the thread calling execute(). Can be used to pull tasks onto a specific thread or have it itself run in a virtual threaded executor for maximum efficiency.
  • Scheduler.shared(): Every Scheduler or Worker can now be shared and act like its own full Scheduler with lifecycle tracking and dispose support. I.e., share one worker of cached() like it is some kind of Schedulers.single().

In 4.x, the traditional I/O scheduler Schedulers.io() has been API deprecated and delegates to Schedulers.cached() for compatibility reasons. It is recommended you decide at these deprecated code locations which standard (or custom) scheduler to use: cached() like before or the new virtual() for more efficient system resource usages.

These are available on all JVM platforms but some specific platforms, such as Android, have their own typical Schedulers defined: AndroidSchedulers.mainThread(), SwingScheduler.instance() or JavaFXScheduler.platform().

In addition, there is an option to wrap an existing Executor (and its subtypes such as ExecutorService) into a Scheduler via Schedulers.from(Executor). This can be used, for example, to have a larger but still fixed pool of threads (unlike computation() and io() respectively).

The Thread.sleep(2000); at the end is no accident. 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 in th

Issues· 13 open

View all issuesOpen on GitHub
  • #8311

    Suspicious entropy detected in recent push (5.546)

    entropysecurityreview-neededUpdated Sep 28, 2026
  • #8299

    4.x: Streamable operator checklist

    Enhancement4.xStreamableAIUpdated Sep 22, 2026
  • #6132

    Update wiki to reflect 4.x (tracking issue)

    DocumentationDiscussionPR welcome4.xUpdated Aug 19, 2026
  • #8280

    4.x: FlowableBufferTest > longTimeAction() FAILED

    Test-Failures4.xUpdated Aug 10, 2026
  • #8075

    4.x: Separate `TestHelper.race` uses so they can run `@Isolated`

    Test-FailuresTestPR welcome4.xUpdated Aug 3, 2026
  • #6931

    4.x: parallel performs poorly with 10+ parallelism

    Performance4.xUpdated Jul 27, 2026
  • #7912

    4.x: Cache/Replay operators to be more GC friendly

    Enhancement4.xUpdated Jul 27, 2026
  • #8089

    4.x: Java target version clarification

    Discussion4.xUpdated Mar 30, 2026
  • #5813

    4.x: Flowable missing or incorrect marbles (tracking issue)

    DocumentationPR welcome4.xUpdated Mar 25, 2026
  • #7548

    Feature Request: Add `Observable.usingWhen()` for resources generated by a `Publisher`

    Feature-Request4.xUpdated Mar 25, 2026

> Tags

Javaflowjavareactive-streamsrxjava

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架