jQuery in Action, 3rd edition
The moose likes Threads and Synchronization and the fly likes Future object with Runnable interface Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Future object with Runnable interface" Watch "Future object with Runnable interface" New topic

Future object with Runnable interface

Rajat Jindal
Ranch Hand

Joined: Sep 19, 2011
Posts: 44
What is the exact use of Future class in java.util.concurrent package?
Can we use it for Runnable interface instead of Callable interface which does not return any value, if yes, how?

I can see following method in java.util.concurrent.ExecutorService class

Since java.util.Runnable interface has only run() method and which cannot return any value , what will be Future<?> object's use here and what value Future object will catch/wrap/store in it for Runnable interface?
rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 132


As specified in => FUTURE <= A Future represents the result of an asynchronous computation.As far as my understanding goes... The task you are trying to perform asynchronously, will go in as a Runnable task to e.g. a complex calculation . This will return a future object like . Now if you want to check whether the calculation (being done by the thread that has spawned for the runnable task) is over, you can call . However,this Thread (which is carrying out the invocation of get()) waits if necessary for the computation to complete, and then retrieves its result.

Please post if you have any particular problem, that needs a solution.

Matthew Brown

Joined: Apr 06, 2010
Posts: 4543

(See also Jesper's response in the other version of this thread: http://www.coderanch.com/t/579972/java/java/Future-class-concurrent-package-Runnable. But I'd suggest continuing any discussion here).
Rajat Jindal
Ranch Hand

Joined: Sep 19, 2011
Posts: 44
I saw answers on both the threads but still not able to identify in which case

submit() method which takes Runnable Object as argument can return Future object. For Callable interface it makes sense but how it works with Runnable.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15081

I gave an answer to that in the other copy of your question:
Jesper de Jong wrote:A Runnable indeed does not return a value, so for a Runnable, a Future is not that useful. It's still useful in that case to find out if the asynchronous task has failed with a runtime exception (get() would throw an ExecutionException in that case), or if the task has been executed, which you can check with the isDone() method of Future.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link: http://aspose.com/file-tools
subject: Future object with Runnable interface
It's not a secret anymore!