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

Oleksandr Otenko oleksandr.otenko at oracle.com
Mon Apr 14 16:18:34 EDT 2014


What's your expected behaviour with this Fibonacci example?

Do you mean it fails for some small values of args[0] with some build of 
Java 8?

Alex

On 14/04/2014 16:41, Paul Sandoz wrote:
> 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());
>     }
> }
>
>
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest

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


More information about the Concurrency-interest mailing list