I' m reading scala.concurrent.impl.Promise and get confused by the concept "link DefaultPromise with another". I do understand the 'prevent memory leak' part but don't know how to write an example where the current state of DefaultPromise is DefaultPromise.
When DefaultPromise is first created, its state is Nil and the list grows as we keep appending flatMap to it. Under what circumstances is the branch case dp: DefaultPromise[_] => dp.asInstanceOf[DefaultPromise[S]].linkRootOf(p) in Future.flatMap get called?
test("default promise linking") {
//f1.getState === Nil
val f1 = Future {Thread.sleep(200000); 2}
//f1.getState === List(CallbackRuer1)
f1.flatMap(x => Future {Thread.sleep(200000); 4})
//f1.getState === List(CallbackRuer2, CallbackRuer1)
f1.flatMap(y => Future {Thread.sleep(200000); 3})
Thread.sleep(30000000)
}
