RxJava

RxJava is a library for writing event-based, asynchronous code using observable streams. RxJava provides a lot of functionality and is quite powerful. However, it can also be used for simple asynchronous code as well.

A simple example of using RxJava to run some work on a background thread can be seen in the following code snippet:

Single.fromCallable {    Thread.sleep(5000)    "Single is done"}.observeOn(Schedulers.io()).subscribe { value -> println(value) }

With RxJava, we can define how our events will be emitted, which schedules should be used to run the work, and then manage the completion of that work. This is quite similar to the CompletableFuture API we looked at previously.

With RxJava, we can also perform more advanced operations ...

Get Mastering Kotlin now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.