Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

What Does java.util.concurrent.Future Hold?

DZone's Guide to

What Does java.util.concurrent.Future Hold?

· Java Zone ·
Free Resource

Verify, standardize, and correct the Big 4 + more– name, email, phone and global addresses – try our Data Quality APIs now at Melissa Developer Portal!

Let’s be sure of what the Future holds

Future, which is a part of the Java concurrency Task execution framework, is the result of your computation as the javadoc claims. And more.

When you are executing a task in a different thread, there is a lot of information you would need from it soon after you submit it to the pool.

What can the Future give?

  1. Result : If you are spawning multiple threads and you are interested in the consolidated/incremental result, obviously you wanted a way to get it. So, here you go. Future.get()

  2. Cancel : Just in case if you spawned 10 threads and you are happy with just one result and wanted to kill the others (or) you wanted to kill that rogue thread (or) you simply loved to power to cancel any thread at will, you have a nice Future.cancel().

  3. Status : Future.isDone() is your way of asking Are you done with your work?

Interesting fact :

Future.get() blocks

The Future.get() method waits for the result forever until the result gets returned. For some cases, you might need this. However, if you don’t want to wait forever, you might choose the overloaded get method

V get(long timeout, TimeUnit unit) 

 

Fun Fact :

If you are just spawning a single thread in your Executor and calling the Future.get() (the one which waits forever), you are actually mimicking a Thread.join() call on a spawned single thread by the main thread.

Further reading :

Alex Miller’s amazing blog

Alex Miller’s concurrency gotchas

Vogella’s article on comparison with original API

Good introduction to concurrency in general

Highly recommended book on Java concurrency

Developers! Quickly and easily gain access to the tools and information you need! Explore, test and combine our data quality APIs at Melissa Developer Portal – home to tools that save time and boost revenue. Our APIs verify, standardize, and correct the Big 4 + more – name, email, phone and global addresses – to ensure accurate delivery, prevent blacklisting and identify risks in real-time.

Topics:

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}