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

Jörg Hettel joerg.hettel at hs-kl.de
Fri Jul 24 11:32:55 EDT 2015


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;
   }
}



More information about the Concurrency-interest mailing list