File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Future object with Runnable interface" Watch "Future object with Runnable interface" New topic
Author

Future object with Runnable interface

Rajat Jindal
Ranch Hand

Joined: Sep 19, 2011
Posts: 42
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

Hi,

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.

Cheers,
Rohit
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

(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: 42
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: 14278
    
  21

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Future object with Runnable interface