When a consumer subscribes, the given io.reactivex.function.Action
is invoked and the consumer completes or receives the exception the Action
threw.
Runnable runnable = () -> System.out.println("Hello World!"); Completable completable = Completable.fromRunnable(runnable); completable.subscribe(() -> System.out.println("Done"), error -> error.printStackTrace());
Note: the difference between
fromAction
andfromRunnable
is that theAction
interface allows throwing a checked exception while thejava.lang.Runnable
does not.
The Callable interface is similar to Runnable, in that both are designed for classes whose instances are potentially executed by another thread. A Runnable, however, does not return a result and cannot throw a checked exception.
References
https://github.com/ReactiveX/RxJava/wiki/Creating-Observables#fromrunnable
https://stackoverflow.com/questions/141284/the-difference-between-the-runnable-and-callable-interfaces-in-java