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/flatMapwith a functionfon a FutureA, you will just create a new FutureBthat will be the result of the previous one transformed by the functionf.Note that:
Ais not yet ‘filled’ you will have a not yet filledBainAwill also executef(a)and set the value inBAis already ‘filled’ then the caller of map/flatMap will also executef(a), but it will NOT recompute the value ofA.onSuccess/onFailurethat will just register some code to be executed when the future gets its value.