• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Callable vs Runnable (general opinion)

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Master Rancher
Posts: 4806
72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic