Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.get() vs .join() #563

Open
Mildred-ui opened this issue Nov 29, 2020 · 2 comments
Open

.get() vs .join() #563

Mildred-ui opened this issue Nov 29, 2020 · 2 comments
Labels
🖱️ Java General Questions with regards to Java or its API

Comments

@Mildred-ui
Copy link

Hi all!

I was looking at API for CompletableFuture, then I realized the .get() and .join() method are pretty similar.

.get() : Waits if necessary for this future to complete, and then returns its result.
.join(): Returns the result value when complete, or throws an (unchecked) exception if completed exceptionally.

I just wonder if there's any rules for using these two methods in different situation. Like is get() better than join() in some cases?

Thanks!

@Mildred-ui Mildred-ui added the 🖱️ Java General Questions with regards to Java or its API label Nov 29, 2020
@naranghardik16
Copy link

naranghardik16 commented Nov 30, 2020

the only difference is how methods throw exceptions. get() is declared in Future interface as

V get() throws InterruptedException, ExecutionException;

The exceptions are both checked exceptions which means they need to be handled in the code itself.

The join() method doesn't throw checked exceptions.

public T join()

Instead it throws unchecked CompletionException.

@sharleneq
Copy link

To add on, perhaps you can read up on this which provides an example of handling of the exceptions for get() and join() respectively.

https://stackoverflow.com/questions/45490316/completablefuture-join-vs-get

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🖱️ Java General Questions with regards to Java or its API
Projects
None yet
Development

No branches or pull requests

3 participants