I'm a newbie in Finagle. I'm now reading someone's code and found that a Future object is reused in different join operations. My question is will that cause the Future object to be executed multiple times (in each join), or will it only execute once and store the result for later joins?
Example:
Future<A> a= b
.join(c)
.flatMap(new SomeFunctionReturningA());
Future<Tuple2<A, B>> future1 = a.join(b);
Future<D> future2 = future1.flatMap(new SomeFunctionReturningD());
future2.get();
So will b be executed twice, or just once?
A Future is just a container for a value, and the value will only be set one time!
The Future[T]
have different states:
When you use map/flatMap
with a function f
on a Future A
, you will just create a new Future B
that will be the result of the previous one transformed by the function f
.
Note that:
A
is not yet 'filled' you will have a not yet filled B
a
in A
will also execute f(a)
and set the value in B
A
is already 'filled' then the caller of map/flatMap will also execute f(a)
, but it will NOT recompute the value of A
.onSuccess/onFailure
that will just register some code to be executed when the future gets its value.