0

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.

Jim C
  • 3,957
  • 25
  • 85
  • 162

1 Answers1

2

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.

NoNameProvided
  • 8,608
  • 9
  • 40
  • 68
  • @NoNomeProvided, thanks. I am wondering how precise is the second part of your comment (maybe very accurate if we ignore Java 8 CompletableFuture). From https://www.callicoder.com/java-8-completablefuture-tutorial/ we read: For building asynchronous systems we should be able to attach a callback to the CompletableFuture which should automatically get called when the Future completes. That way, we won’t need to wait for the result, and we can write the logic that needs to be executed after the completion of the Future inside our callback function. – Jim C Nov 17 '17 at 05:51
  • Future readers may enjoy watch https://www.youtube.com/watch?v=IwJ-SCfXoAU especialy the second part of presentation around 1:30 time – Jim C Dec 24 '17 at 04:35