Junior — Middle
Can you list the types of execution threads used in RxJava?
sobes.tech AI
Answer from AI
RxJava uses different types of streams (Schedulers) to control which thread operations are performed on:
-
Schedulers.io() — for input/output operations (network requests, file handling). Uses a thread pool that can expand as needed.
-
Schedulers.computation() — for computational tasks, uses a fixed thread pool equal to the number of processor cores.
-
Schedulers.newThread() — creates a new thread for each task.
-
Schedulers.single() — uses a single thread for sequential task execution.
-
AndroidSchedulers.mainThread() (in Android) — for executing operations on the main (UI) thread.
Example usage:
Observable.just("data")
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(data -> {
// handle on UI thread
});