fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import java.util.*;
  7. import java.lang.*;
  8. import java.io.*;
  9.  
  10. import java.util.concurrent.CompletableFuture;
  11.  
  12. /* Name of the class has to be "Main" only if the class is public. */
  13. class Ideone
  14. {
  15. public static void main(String[] args) throws Exception {
  16. runTasks(2);
  17. runTasks(0);
  18. }
  19.  
  20. private static void runTasks(int i) {
  21. System.out.printf("-- input: %s --%n", i);
  22. CompletableFuture
  23. .supplyAsync(() -> {
  24. return 16 / i;
  25. })
  26. .supplyAsync(() -> {
  27. System.out.println("I WAS HERE");
  28. System.err.println("I WAS HERE");
  29. return i;
  30. })
  31. .whenComplete((input, exception) -> {
  32. if (exception != null) {
  33. System.out.println("exception occurs");
  34. System.err.println(exception);
  35. } else {
  36. System.out.println("no exception, got result: " + input);
  37. }
  38. })
  39. .thenApply(input -> input * 3)
  40. .thenAccept(System.out::println);
  41.  
  42. }
  43. }
  44.  
Success #stdin #stdout #stderr 0.16s 62592KB
stdin
Standard input is empty
stdout
-- input: 2 --
I WAS HERE
no exception, got result: 2
6
-- input: 0 --
I WAS HERE
no exception, got result: 0
0
stderr
I WAS HERE
I WAS HERE