Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Future object with Runnable interface

 
Rajat Jindal
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4565
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15207
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic