[concurrency-interest] Repeatedly completing futures?

Benjamin Sieffert benjamin.sieffert at metrigo.de
Thu Sep 11 04:28:51 EDT 2014


Hello there,

I think the guarantees concerning the number of times a Future will be
completed/fail is very important, because it goes a long way to prevent
multithreading mistakes when using them. E.g. if Future didn't come with
this kind of guarantee and one wanted to trigger a callback logic only
once, a naive implementation would be "if(! future.isDone()) {
future.complete(..) }" which is obviously wrong.

The mechanism you are looking for, though, seems to be the kind of
continuous pushing/polling mechanism that is commonly implemented by using
queues. That is, your call to .complete() is rather like a .put() and your
call to .get() is rather like a .take().
In regards to the functions you'd like to apply to the future's result
along the way, then, I think there's a lot of frameworks out there that let
you build these kinds of pipelines. You might especially want to look into
actor oriented programming.

Regards
Benjamin

On 10 September 2014 18:10, Millies, Sebastian <
Sebastian.Millies at softwareag.com> wrote:

>  Hello there,
>
>
>
> is there something like a CompletableFuture that can  be completed
> multiple times?
>
> I’d like to react to repeatedly occurring events, always with the same
> chain of asynchronous processing.
>
> Here is a simulation of what I have in mind:
>
>
>
>     Supplier<Double> randomNumberSupplier = …;
>
>     CompletableFuture<Double> future =
> succeedRepeatedlyWithFixedDelay(randomNumberSupplier, 100, MILLISECONDS);
>
>     future = future
>
>                .thenApplyAsync(this::doSth)
>
>                .thenApplyAsync(this::doMore);
>
>     while( true ) {
>
>       double result = future.get(); // block until result of processing
> next event becomes available
>
>       publish(result);
>
>     }
>
>
>
>     static <T> CompletableFuture<T>
> succeedRepeatedlyWithFixedDelay(Supplier<T> generator, int delay, TimeUnit
> unit) {
>
>       CompletableFuture<T> future = new CompletableFuture<T>();
>
>       try {
>
>         scheduler.scheduleWithFixedDelay(() ->
> future.complete(generator.get()), delay, delay, unit); // <-- HERE
>
>       } catch (RejectedExecutionException e) {
>
>         future.completeExceptionally(new CompletionException("Could not
> schedule timer task", e));
>
>       }
>
>       return future;
>
>     }
>
>
>
> But of course CompletableFuture can be completed only once.
>
> So my questions are: how to go about this? Why cannot CF be completed
> repeatedly?
>
>
>
> n  Sebasian
>
>
>
> *Sebastian Millies*
>
> *Expert Java Business Analytics*
>
> Phone: +49 681 210 3221 *|* Fax: +49 681 210 1801 *|*
> Sebastian.Millies at softwareag.com <Sebastian.Millies at ids-scheer.com>
>
>
>
>    Software AG – Sitz/Registered office: Uhlandstraße 12, 64297
> Darmstadt, Germany – Registergericht/Commercial register: Darmstadt HRB
> 1562 - Vorstand/Management Board: Karl-Heinz Streibich
> (Vorsitzender/Chairman), Dr. Wolfram Jost, Arnd Zinnhardt; -
> Aufsichtsratsvorsitzender/Chairman of the Supervisory Board: Dr. Andreas
> Bereczky - *http://www.softwareag.com* <http://www.softwareag.com>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
>


-- 
Benjamin Sieffert
metrigo GmbH
Sternstr. 106
20357 Hamburg

Geschäftsführer: Christian Müller, Tobias Schlottke, Philipp Westermeyer,
Martin Rieß
Die Gesellschaft ist eingetragen beim Registergericht Hamburg
Nr. HRB 120447.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140911/c94d1bf1/attachment-0001.html>


More information about the Concurrency-interest mailing list