Skip to content

Instantly share code, notes, and snippets.

@theboreddev
Created October 23, 2022 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theboreddev/ec4bb3e83da95741f1e1c8555255557c to your computer and use it in GitHub Desktop.
Save theboreddev/ec4bb3e83da95741f1e1c8555255557c to your computer and use it in GitHub Desktop.
unstructured-completablefuture
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