Search code examples
javaangularrxjsobservablecallable

from client implementation perspective, can I compare Java Callable to Angular Observable (RxJS)


Before someone think to downvote or even close my question I would like to highligh that I am not asking which is better (cetainly a non-sense question especially when we think one is focused to server and other to browser side).

From http://winterbe.com/posts/2015/04/07/java8-concurrency-tutorial-thread-executor-examples/ we see this simple example:

ExecutorService executor = Executors.newFixedThreadPool(1);
Future<Integer> future = executor.submit(task);

System.out.println("future done? " + future.isDone()); // prints future done? false

Integer result = future.get();

System.out.println("future done? " + future.isDone()); // prints future done? true
System.out.print("result: " + result); //result: 123

Well, adding the idea that I can control when call future.get() is pretty the same idea happening with Observable in practical terms. I mean, for instance, I can call a rest service using callable and then get the result after some time without blocking my code and control the maximun time (seconds) I will accept this lazy beaviour.

I read carefully Difference between Java 8 streams and RxJava observables and it is clear that Java streams are quite different from Angular observables but I am wondering if I got correct the idea of Java callable and ExecutorService.


Solution

  • I am not a Java person, but for me Callables seems to be the counterpart of Promises in Javascript.

    In very short and rough comparison, the main difference are:

    • Promises and Callables are pull-based, while Observables are push-based,
    • Promises and Callables emit data only once, then complete, Observables emits multiple times

    Well, adding the idea that I can control when call future.get() is pretty the same idea happening with Observable in practical terms.

    No, as a consumer, controlling when to call get is the same as to control when to call then on a Promise. You cannot control when an observable will emit as a consumer.

    But ofc, RxJS differs in a lot of other ways, but these are the main reasons.