javamultithreadingfiberquasar

Can I assume SettableFuture in quasar as a Fiber?


Can I assume a SettableFuture acting as a Fiber in asynchronous code or not (It should be assumed as a ForkJoinTask)?

A Quasar SettableFuture can block fibers in addition to threads.

SettableFuture API
Parallel Universe Comsat Documentation


Solution

  • A Future does not represent running code but rather a value that could be either immediately available or could become available in the future (hence the name) concurrently w.r.t. the thread(s) holding a reference to it. In addition it can be awaited for and the awaiting thread(s) will block in the meanwhile: this means it also acts as a threads synchronization mechanism.

    A "settable" future is one whose value can be not only produced internally by the implementation class but also set from an external thread, similarly to a promise.

    Quasar's SettableFutures have the additional ability to allow all types of Quasar strands (i.e., at present, regular Java threads as well as Quasar fibers) to block if they await on it when the value is not yet available.

    This means you can create a SettableFuture in any strand (thread or fiber), reference it e.g. in the code of an async callback that will set its value (when executed) but return it immediately for more strands (threads or fibers) to block while awaiting its value. It is thus an excellent tool to convert async APIs into blocking (typically efficient fiber-blocking) APIs. This pattern too is described in the "Future" sub-section of this blog post.