fork(1) download
  1.  
  2. import java.util.concurrent.CompletableFuture;
  3. import java.util.concurrent.ExecutionException;
  4.  
  5. class Ideone {
  6. public static void main (String[] args) throws Exception {
  7. Thread.currentThread().interrupt();
  8.  
  9. try {
  10. CompletableFuture.runAsync(() -> interruptibleComputation())
  11. .whenComplete((r, t) -> System.out.println("foo")).get();
  12. }
  13. finally {
  14. // Prevent the JVM from terminating immediately (which would release the resources anyway
  15. Thread.sleep(4000);
  16. }
  17.  
  18. }
  19.  
  20. static void interruptibleComputation() {
  21. try {
  22. Thread.sleep(3000);
  23. } catch (InterruptedException ex) {
  24. // will never happen, as CompletableFuture does not support interruption
  25. ex.printStackTrace();
  26. }
  27. }
  28. }
Runtime error #stdin #stdout #stderr 0.08s 34204KB
stdin
Standard input is empty
stdout
foo
stderr
Exception in thread "main" java.lang.InterruptedException
	at java.base/java.util.concurrent.CompletableFuture.reportGet(CompletableFuture.java:385)
	at java.base/java.util.concurrent.CompletableFuture.get(CompletableFuture.java:2070)
	at Ideone.main(Main.java:11)