• Post Reply Bookmark Topic Watch Topic
  • New Topic

Callable vs Runnable (general opinion)  RSS feed

 
Ted Jones
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm creating a program. As I've been puttering along, I've run into a bit of a snag.

My intention is to have a program that has sort of a brain. I have it as my controller right now. I'll have it get data, reboot and login. I want it to be able to handle an error from a thread such as file not found. For instance, the computer screen locks up, I want it to see that a lockup occurred in my VM (which I'm alread doing) and do a reboot. It works as I push a button and see an exception in the console, but I'm lazy and I want my computer to do it.

I only have one thread active at a time, so I'm not too worried about sync errors.


What I was thinking about extending ThreadPoolExecutor and creating an actionlistener in an overridden afterExecute. That way, I can look through my actionListener for issues and do a screenshot if it finds one to help me. However, I was reading that I can possible slow the program if I'm running a long process in afterExecute (since it is still a part of the child thread...).

However, as a part of my research, I've found something called Callable. I've never really seen it, so I'm not sure about it. Should I spend more time researching Callable or continue with my current thought?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to do something with the result, go for a Callable!
 
Ted Jones
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,

I don't have or care about a result. I just want to handle exceptions. Does that change your answer?
 
Joe Harry
Ranch Hand
Posts: 10128
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ted Jones wrote:Joe,

I don't have or care about a result. I just want to handle exceptions. Does that change your answer?


With Runnable you cannot throw checked exceptions. So you are better off using a Callable for what you want!
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that if you want to handle the exception, you will need to look at the result, even if you don't otherwise care about the result. In fact the result will probably be null. But in order to handle the exception, you need to take the Future corresponding to your Callable (the one returned by the ExecutorService when you submitted your task), and call get() on it. This get() will throw an exception if the Callable threw an exception.

This is not intended as a complete explanation of Callable and Future. But as you read about the topic, pay attention to how the Future is used with a Callable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!