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

Callable/Future Real world Example

 
anuj guptaa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
anuj guptaa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Steve!! Hope i will soon get a chance to use callable in my project..
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is an example of using Callable with a Thread Pool in this FAQ
 
anuj guptaa
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jayesh!! This link is quiet helpful !!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic