-
-
Save theboreddev/ec4bb3e83da95741f1e1c8555255557c to your computer and use it in GitHub Desktop.
unstructured-completablefuture
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void main(String[] args) throws InterruptedException { | |
CompletableFuture<Void> future1 = CompletableFuture.runAsync(() -> { | |
try { | |
Thread.sleep(2000); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
System.out.println("I'm task 1 running on " + Thread.currentThread().getName()); | |
}); | |
CompletableFuture<Void> future2 = CompletableFuture.runAsync(() -> { | |
try { | |
Thread.sleep(2000); | |
} catch (InterruptedException e) { | |
throw new RuntimeException(e); | |
} | |
System.out.println("I'm task 2 running on " + Thread.currentThread().getName()); | |
}); | |
CompletableFuture<Void> future3 = CompletableFuture.runAsync(() -> { | |
System.out.println("I'm task 3 running on " + Thread.currentThread().getName()); | |
throw new RuntimeException("Something went wrong!"); | |
}); | |
CompletableFuture.allOf(future1, future2, future3) | |
.join(); | |
System.out.println("All tasks started!"); | |
Thread.sleep(3000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment