When a consumer subscribes, the given java.util.concurrent.Callable
is invoked and its returned value (or thrown exception) is relayed to that consumer.
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.
Callable<String> callable = () -> { System.out.println("Hello World!"); return "Hello World!"); } Observable<String> observable = Observable.fromCallable(callable); observable.subscribe(item -> System.out.println(item), error -> error.printStackTrace(), () -> System.out.println("Done"));
Remark: In Completable
, the actual returned value is ignored and the Completable
simply completes.
References
https://github.com/ReactiveX/RxJava/wiki/Creating-Observables#fromcallable
https://stackoverflow.com/questions/141284/the-difference-between-the-runnable-and-callable-interfaces-in-java