r/javahelp • u/akanshya9z12 • Jun 02 '21
Codeless What is Future class in java?
I saw this class being used in a project recently, which was handling a parallel processing request. Please explain to me in layman terms what is the purpose of this class?
7
u/HecknChonker Extreme Brewer Jun 02 '21
Often times you want to be able to run multiple functions at the same time for performance reasons. A java process can spin up a child thread to run a function in a child thread to run that process, and then you can use a Future to pass the result of of that function from a child thread back to the the calling thread.
You can't pass the value back at the time you spin up the child thread because the value might not be ready yet. So instead java wraps the return value in a Future, and you can use that to access the value once it's ready. If you try to access the value before it's ready the main thread will wait until the child thread is finished processing, and then the main thread will continue again.
This is a the pattern I typically use.
public Future<Integer> countThings(List<Thing> things) {
return CompletableFuture.supplyAsync(() -> {
// Normally this would be doing something more complex than just returning the size of a list.
// Like running a database query, calling an external API, or maybe downloading a file.
// The value returned here will be passed back to the main thread via a Future.
return things.size();
});
}And I would use it like this:
// This will create 2 background threads and start processing both.
Future<Integer> countOfThings = countThings(things);
Future<Integer> countOtherThings = countThings(otherThings);
// Other processing can continue to happen here on the main thread
// While this is happening, the two Future's are being processed in separate background threads until they return.
// When you call .get() on a future the main thread will wait until the background processing is complete.
// If the background processing finished before you call .get() the value will return immediately.
Integer thingsCount = countOfThings.get();
Integer otherThingsCount = countOtherThings.get();Let me know if this helps or if you have more questions.
1
4
u/NautiHooker Software Engineer Jun 02 '21
I think the docs do a good job of explaining this.
https://docs.oracle.com/javase/7/docs/api/java/util/concurrent/Future.html
2
u/wildjokers Jun 02 '21
Probably should link to newer JavaDoc:
https://docs.oracle.com/en/java/javase/16/docs/api/java.base/java/util/concurrent/Future.html
1
u/NautiHooker Software Engineer Jun 02 '21
True. I just picked the first one from Google. Didnt pay attention to the version. Thanks!
4
2
u/Prateeeek Jun 02 '21
It's like promises in JavaScript, when the result can't be returned right away because an asynchronous task is happening we return a future. That future would then have the value later in time when the asynchronous task has returned a result. We would then use future.get to get the actual value.
•
u/AutoModerator Jun 02 '21
Please ensure that:
Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions
You include any and all error messages in full
You ask clear questions
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://imgur.com/a/fgoFFis) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.