RxJava single background thread scheduler
You can create a single reusable thread to create a Scheduler
for the Observable
in one of the following ways:
- Create a
ThreadPoolExecuter
with a pool size of 1 (Executors.newSingleThreadExecutor()
is a convenient static factory method for doing that), then use it to generate the schedulers via theSchedulers.from()
method. - RxAndroid provides a custom
Scheduler
implementation that uses aHandler
to schedule the actions, and thus can be used with anyThread
that has aLooper
running by passing it'sHandler
to theAndroidSchedulers.handlerThread()
factory method.
Note that you will need to observe on a main thread Scheduler
if you're interacting with the UI at the conclusion of these tasks.
In RxJava 2 you can use Schedulers.single()
which:
Returns a default, shared, single-thread-backed Scheduler instance for work requiring strongly-sequential execution on the same background thread.
Please see the documentation for more details.
I don't see it available in RxJava 1 Schedulers documentation.