• Post Reply Bookmark Topic Watch Topic
  • New Topic

Callable and executor Framework  RSS feed

 
avishek bose
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi,


Can any one tell me when to use runnable and when to use callable in case of threads, i mean i know callable returns a value but only for that can be replace callable with runnable...

secondly can somebody let me know how to use callable in executor framework.

regards
Avishek
 
Stephan van Hulst
Saloon Keeper
Posts: 7993
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, that's pretty much it. Callable can do anything that a Runnable can do. The only (but important) benefit that Runnable has, is that it's nice and simple. If your task doesn't have a return value, then Runnable is better.

Using Callable is pretty simple. You first need an ExecutorService. You can obtain one preferably through the Executors utility class. Then you just submit() your Callable to the service.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stephan van Hulst wrote:Well, that's pretty much it. Callable can do anything that a Runnable can do. The only (but important) benefit that Runnable has, is that it's nice and simple. If your task doesn't have a return value, then Runnable is better.
.


Agreed. And it is a bit weird that the difference is that minor. My guess is because Runnable was created first. If Callable came first, I would doubt that the Runnable interface would have been added.

Henry
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you're 100%. Runnable has nothing Callable doesn't have, but Callable adds two features that can be very useful: a generic return type*, and it can throw exceptions.

* Object / Void can be used for the type and null as the value if it's not needed.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!