• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Future object with Runnable interface

 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(See also Jesper's response in the other version of this thread: https://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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic