[concurrency-interest] CompletableFuture: Which thread runs the thenApply-Function?

Gregg Wonderly gergg at cox.net
Sat Jul 25 20:02:28 EDT 2015


I see behaviors which have to do with the thread scheduling quanta time.  If you use async in a chain, what do you expect the results to look like for the value returned by cf?

Gregg Wonderly

> On Jul 24, 2015, at 10:32 AM, Jörg Hettel <joerg.hettel at hs-kl.de> wrote:
> 
> Hi everyone,
> 
> the behaviour of a sequence build of CompletableFuture with non-async methods depends on the “duration” of the tasks.
> 
> The documentation says:
> “Actions supplied for dependent completions of non-async methods may be performed by the thread that completes the current CompletableFuture, or by any other caller of a completion method.“
> 
> If I run the test program below on my machine, I get different outputs, if the first task has different runtimes.
> 
> This raises the following question for me:
> 
> 1. It would therefore be a good idea always to use the then<XXXX>Async method, right?
> 2. What was the intent to include in each case async and non-async versions of the methods in the API?
> 3. In which cases can I safely use non-async methods to build asynchronous sequences?
> 
> Regards
> 
> Joerg
> 
> ===========================================================================
> 
> With work(10000);
> 
> Thread[ForkJoinPool.commonPool-worker-1,5,main]
> Thread[main,5,main]
> Thread[main,5,main]
> Thread[main,5,main]
> done
> ----------------------------------------------------------------
> 
> With work(20000);
> 
> Thread[ForkJoinPool.commonPool-worker-1,5,main]
> Thread[ForkJoinPool.commonPool-worker-1,5,main]
> Thread[main,5,main]
> Thread[main,5,main]
> done
> -----------------------------------------------------------------
> 
> With work(60000);
> 
> done
> Thread[ForkJoinPool.commonPool-worker-1,5,main]
> Thread[ForkJoinPool.commonPool-worker-1,5,main]
> Thread[ForkJoinPool.commonPool-worker-1,5,main]
> Thread[ForkJoinPool.commonPool-worker-1,5,main]
> -----------------------------------------------------------------
> 
> public class AsyncCalculation
> {
>  public static void main(String[] args)
>  {
>    CompletableFuture<Void> cf = CompletableFuture.supplyAsync( () -> {
>                               work(10000); // Here change the value
>                               System.out.println( Thread.currentThread());
>                               return 2; } )
>                     .thenApply( r -> { System.out.println( Thread.currentThread()); return r*r; } )
>                     .thenApply( r -> { System.out.println( Thread.currentThread()); return r+2; } )
>                     .thenAccept(r -> { System.out.println(Thread.currentThread() ); } );
> 
>    System.out.println("done");
>    cf.join();
>  }
> 
>  private static long work(int len)
>  {
>    long sum = 0;
>    for(int i=0; i < len; i++ )
>    {
>      sum += Math.sqrt(i);
>    }
>    return sum;
>  }
> }
> 
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest




More information about the Concurrency-interest mailing list