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

Paul Sandoz paul.sandoz at oracle.com
Mon Apr 14 11:41:56 EDT 2014


On Apr 14, 2014, at 8:51 AM, Peter Levart <peter.levart at gmail.com> wrote:

> On 04/14/2014 12:50 AM, Doug Lea wrote:
>> Peter: Maybe you could post one of your examples as
>> a sample test program.
>> 
>> -Doug
> 
> Sure, here's an artificial example that provokes StackOverflowError in previous version (CVS:1.99):
> 
> 

Very nice! that melts my laptop :-) below is a version that reproduces the same problem producing less heat.

I originally thought that the tail recursion fix would cover most cases, but as you have shown it is quite easy to create a linked list of completions > 1 node.

Relevant jtreg unit tests pass with the latest fix.
 
Paul.


import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.function.*;

public class FibonacciStackOverflow {

   static Future<BigInteger> fib(
       CompletableFuture<BigInteger> f0,
       CompletableFuture<BigInteger> f1,
       int n) {
       if (n < 0) throw new IllegalArgumentException("n should be >= 0");

       f0.thenAccept(i -> {});
       if (n > 0) {
           f1.thenAccept(v -> {});
           for (int i = 2; i <= n; i++) {
               f1 = f0.thenCombine(f0 = f1, BigInteger::add);
               f1.thenAccept(v -> {});
           }
       }

       return f1;
   }

   public static void main(String[] args) throws Exception {
       CompletableFuture<BigInteger> f0 = new CompletableFuture<>();
       CompletableFuture<BigInteger> f1 = new CompletableFuture<>();

       int n = Integer.valueOf(args[0]);
       Future<BigInteger> fn = fib(f0, f1, n);

       f0.complete(BigInteger.ZERO);

       f1.complete(BigInteger.ONE);  // Trigger SOE

       System.out.println(fn.get());
   }
}

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 841 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20140414/48c9db12/attachment.bin>


More information about the Concurrency-interest mailing list