Skip to content
Home » RxJava fromRunnable operator

RxJava fromRunnable operator

  • by
RxJava fromCallable operator

1. Overview

In this article, we will discuss the RxJava fromRunnable operator with a few examples.

2. Callable

A Runnable is a core interface and the implementing classes execute in threads. The Callable is like Runnable declared in the java.util.concurrent package and runs on the threads from the thread pool such as ExecutorService.

A Runnable, however, does not return a result and cannot throw a checked exception whereas Callable interface contains a single, no-argument call() method that returns a result or throws Exception if unable to compute the result. See this article for more details on Runnable and Callable.

2. RxJava from operator

The RxJava from operator constructs a sequence from a pre-existing source or generator type. The from is like Just, but note that From will dive into an array or an iterable or something of that sort to pull out items to emit, while Just will simply emit the array or iterable or what-have-you as it is, unchanged, as a single item.

See Just operator.

The fromRunnable is a static method and variant of this from operator that accepts a Runnable as input.

It uses the postfix naming convention (i.e., the method name contains the argument type). Here, Iterable is the argument type, and the method name fromRunnable contains Iterable to avoid overload resolution ambiguities.

3. RxJava fromRunnable operator

When a consumer subscribes, it invokes the provided Runnable, and the consumer completes or receives the exception the Runnable threw.

Since the Runnable does not return any result, only onError or onComplete callbacks are invoked and do not emit any value.

 public static <T> Maybe<T> fromRunnable(@NonNull Runnable run)

It can create the RxJava flow types MayBe, Completable.

3.1. RxJava Completable fromRunnable example

Completable represent Observable that emits no value, but only terminal events, either onError or onCompleted.

@Test
    public void testFromRunnable() {
        Runnable runnable = () -> System.out.println("Hello World!");

        Completable completable = Completable.fromRunnable(runnable);

        completable.subscribe(() -> System.out.println("Done"), error -> error.printStackTrace());
    }

So here the Completable ignores the actual returned value and the Completable simply completes.

If you execute the above test case, it prints the following result in the console. As you can see, the Completable invokes the Runnable and calls the onComplete callback.

Hello World! (printed from runnable)
Done

The completable does not have onNext for the consumer as they emit no elements.

3.2. RxJava MayBe fromRunnable example

The Maybe type represents a stream that can emit a single value, complete in an empty state, or report an error.

@Test
    public void testMayBeFromRunnable() {
        Runnable runnable = () -> System.out.println("Hello World!");

        Maybe<Runnable> me = Maybe.fromRunnable(runnable);


        me.subscribe(
                x -> System.out.print("Emitted item: " + x),
                ex -> System.out.println("Error: " + ex.getMessage()),
                () -> System.out.println("Completed")
        );
    }

Since the Runnable does not return any result, you can’t find any item being emitted in onSuccess and simply onError or onComplete callbacks are invoked.

Hello World!
Completed

4. Conclusion

To sum up, we have learned the RxJava fromRunnable operator with examples. You can find code samples in our GitHub repository.

Leave a Reply

Your email address will not be published. Required fields are marked *