[concurrency-interest] Java 8 CompletableFuture.thenCombine() hangs
Peter Levart
peter.levart at gmail.com
Mon Apr 14 17:15:21 EDT 2014
On 04/14/2014 10:18 PM, Oleksandr Otenko wrote:
> 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?
I don't know exactly what is the tripping point on JDK 8 Release, but on
my machine it's between 2000 (passes) and 3000 (fails). With Doug's
intermediary fix it should be about 3x higher (only 1 stack frame in one
recursion step vs. 3), but with latest fix it should work for any number.
Peter
>
> 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
>
>
>
> _______________________________________________
> 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/266794ee/attachment.html>
More information about the Concurrency-interest
mailing list