[concurrency-interest] Repeatedly completing futures?

Millies, Sebastian Sebastian.Millies at softwareag.com
Wed Sep 10 12:10:20 EDT 2014


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<mailto: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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140910/a958b105/attachment.html>


More information about the Concurrency-interest mailing list