CompletableFuture eventual race condition
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?
Why not try to create a sscce that demonstrates the problem? Your snippet looks like it ought to work. On Mon, Jul 11, 2016 at 10:44 AM, Francois Green <francois.green@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?
On 11 Jul 2016, at 18:44, Francois Green <francois.green@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?
Your snippet looks correct to me. You could use jstress [1] to try to catch the alleged bug. And in case you succeed, you could then produce a sscce as Martin suggested. -------------------------------------------------------------------------------- [1] http://openjdk.java.net/projects/code-tools/jcstress/
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@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?
participants (3)
-
Francois Green
-
Martin Buchholz
-
Pavel Rappo