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 Callable/Future Real world Example Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
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: 3968
    
  17

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: 2064
    
  22

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 !!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Callable/Future Real world Example
 
Similar Threads
which one is the better way to create a thread
What exctly the Future class in concurrent package of util?
Implementing task result listener in executor framework
java.util.concurrent.Future with callable
How can a checked exception be thrown from run method of a thread ?