GeeCON Prague 2014*
The moose likes Threads and Synchronization and the fly likes Callable/Future Real world Example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Callable/Future Real world Example" Watch "Callable/Future Real world Example" New topic
Author

Callable/Future Real world Example

anuj guptaa
Greenhorn

Joined: Aug 05, 2013
Posts: 26
Hi

I am learning java concurrent package these days. Today i learned about the Callable interface ,Future/FutureTask, Executor Service. I understood what these are and how to use them.

But I would like to know a real life example for the Callable interface as i have mostly used Runnable in my projects.


Has anyone used Callable in their project . Please throw some light here.

Thanks
Anuj
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

anuj guptaa wrote:But I would like to know a real life example for the Callable interface as i have mostly used Runnable in my projects.
A Callable is just like a Runnable, except it returns a value. It is used to provide a simple, Runnable-like API to insert your executable code into FutureTasks, where some return value is expected. Internally, FutureTask probably makes use of Runnable to execute the code, but in addition, will do something with the results (such as notify get()ers that it is ready).

Real-world use would be any long task that produces a result - get something from a database and turn it into an Object, make a long calculation with a result, query a web application, get some data, do something with that data, generate output...


Steve
anuj guptaa
Greenhorn

Joined: Aug 05, 2013
Posts: 26
Thanks Steve!! Hope i will soon get a chance to use callable in my project..
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2383
    
  28

There is an example of using Callable with a Thread Pool in this FAQ
anuj guptaa
Greenhorn

Joined: Aug 05, 2013
Posts: 26
Thanks Jayesh!! This link is quiet helpful !!
 
GeeCON Prague 2014
 
subject: Callable/Future Real world Example