CompletableFuture eventual race condition

Martin Buchholz martinrb at google.com
Mon Jul 11 20:32:19 UTC 2016


In this program there is a race between the program ending and the
computation trying to print from the daemon thread in the forkjoin pool!

import java.util.concurrent.*;
public class HelloWorld {
    public static void main(String[] args) {
        CompletableFuture.supplyAsync(() -> "Hejjo Worjd from Java")
                         .thenApply(s -> s.replaceAll("j","l"))
                         .thenAccept(System.out::println);
    }
}

Try adding a join:

import java.util.concurrent.*;
public class JoinRace {
    public static void main(String[] args) {
        CompletableFuture.supplyAsync(() -> "Hejjo Worjd from Java")
            .thenApply(s -> s.replaceAll("j","l"))
            .thenAccept(System.out::println).join();
    }
}


On Mon, Jul 11, 2016 at 10:44 AM, Francois Green <francois.green at gmail.com>
wrote:

> I sorry for posting in list, but I need an authoritative answer.  I was
> told by one of the developers of another JVM (Ceylon) language that
>
> CompletableFuture.supplyAsync(() -> "Hejjo Worjd from Java")
>         .thenApply(s -> s.replaceAll("j","l"))
>         .thenAccept(System.out::println);
>
> would eventual fail to print if run enough times.  Is this true?
>


More information about the core-libs-dev mailing list