RxJava Tutorials
Learn RxJava step-by-step with these comprehensive tutorials. From basic concepts to advanced techniques, master reactive programming with practical examples.
Tutorial 1: Creating Observables
In this tutorial, we'll learn how to create Observables using different factory methods and understand the core concepts behind Observable creation.
Basic Observable Creation
There are several ways to create Observables in RxJava:
Observable.create()
- Create an Observable from scratchObservable.just()
- Create an Observable that emits a specified itemObservable.fromIterable()
- Convert an Iterable into an ObservableObservable.fromArray()
- Convert an array into an ObservableObservable.fromCallable()
- Create an Observable from a CallableObservable.interval()
- Create an Observable that emits a sequence of integers spaced by a given time interval
1import io.reactivex.rxjava3.core.Observable;23// Creating and subscribing to a simple Observable4Observable<String> observable = Observable.create(emitter -> {5 try {6 emitter.onNext("Hello");7 emitter.onNext("World");8 emitter.onComplete();9 } catch (Exception e) {10 emitter.onError(e);11 }12});1314// Subscribe with separate action handlers15observable.subscribe(16 item -> System.out.println(item), // onNext handler17 error -> error.printStackTrace(), // onError handler18 () -> System.out.println("Completed") // onComplete handler19);
Creating Observables from Common Sources
RxJava provides factory methods to create Observables from common data sources:
From Collections
List[object Object]
From Future
Future[object Object]
Tutorial 2: Working with Schedulers
In this tutorial, we'll explore how to use Schedulers to control threading in RxJava applications, allowing you to perform operations on different threads.
Understanding Schedulers
Schedulers in RxJava determine which threads are used for executing operations and delivering results. They're crucial for managing concurrency and ensuring that operations don't block critical threads like the UI thread.
Key Scheduler Types
Schedulers.io()
Backed by a thread pool that can grow as needed. Ideal for I/O-bound work like network requests and disk operations.
Schedulers.computation()
Fixed thread pool sized to the number of available processors. Best for CPU-intensive work.
Schedulers.newThread()
Creates a new thread for each unit of work. More expensive but sometimes necessary.
Schedulers.single()
Uses a single thread for all operations, ensuring sequential execution.
Controlling the Execution Thread
RxJava provides two main operators for controlling which threads are used:
subscribeOn()
- Specifies the Scheduler on which the Observable will operateobserveOn()
- Specifies the Scheduler on which the Observer will receive notifications
1import io.reactivex.rxjava3.core.Observable;2import io.reactivex.rxjava3.schedulers.Schedulers;34Observable.just("Long running operation")5 .map(s -> {6 System.out.println("Processing on thread: " + Thread.currentThread().getName());7 // Simulate long-running operation8 Thread.sleep(1000);9 return s + " completed";10 })11 .subscribeOn(Schedulers.io()) // Specify thread for the source Observable12 .observeOn(Schedulers.computation()) // Specify thread for downstream operations13 .subscribe(14 result -> System.out.println("Received on thread: " +15 Thread.currentThread().getName() + ", Result: " + result),16 Throwable::printStackTrace17 );
Tutorial 3: Transforming Observables
Learn how to transform emissions using operators like map, flatMap, switchMap, and concatMap.
View TutorialTutorial 4: Filtering Operators
Master filtering emissions with filter, take, skip, distinct, and debounce operators.
View TutorialTutorial 5: Combining Observables
Explore techniques for combining multiple Observables using merge, concat, zip, and combineLatest.
View TutorialTutorial 6: Error Handling
Learn robust error handling techniques using onErrorReturn, onErrorResumeNext, and retry operators.
View TutorialReady for Advanced Topics?
Once you've mastered the basics, dive into advanced RxJava topics like Subjects, Backpressure, Testing, and Custom Operators.