[concurrency-interest] Java 8 CompletableFuture.thenCombine() hangs

Dennis Sosnoski dms at sosnoski.com
Tue Apr 1 19:16:54 EDT 2014


I've run into a situation with CompletableFuture.thenCombine() hanging 
(i.e., the returned future never completing even though the original 
future and the combined one have completed) with the Oracle Java 8 
release for 64-bit Linux. I wanted to run this by the group before 
creating a bug report to make sure I'm not misunderstanding how this is 
supposed to work. Here's the code:

public class CompletableFutureFail {

     private Supplier<Integer> newLambda(int i) {
         return () -> Integer.valueOf(i);
     }

     private Integer run(int n) {
         CompletableFuture<Integer> last = 
CompletableFuture.supplyAsync(newLambda(0));
         for (int i = 1; i < n; i++) {
             last = CompletableFuture.supplyAsync(newLambda(i)) 
.thenCombine(last, Math::max);
         }
         return last.join();
     }

     public static void main(String[] args) {
         CompletableFutureFail fail = new CompletableFutureFail();
         for (int i = 0; i < 100; i++) {
             fail.run(10000);
             System.out.println("Did it " + i);
         }
     }
}

When I run it from the command line it mostly doesn't make it through 
the first call to run(). Sometimes it does, but it generally only makes 
a few passes before hanging. In other circumstances (such as running 
inside Eclipse) I've seen it make it all the way through to the end, 
though most often it also hangs there in the first few passes. I'm 
thinking it might be Hotspot related because of the halting pattern.

Any suggestions?

Thanks,

   - Dennis



More information about the Concurrency-interest mailing list