This week's book giveaway is in the Design forum.
We're giving away four copies of Design for the Mind and have Victor S. Yocco on-line!
See this thread for details.
Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exceptions from Runnable

 
Dave Barnes
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am new to java so not sure if i am going the right way about this. I am using the executor services and my threads are implementing runnable.

What i want to be able to do is return results or any exceptions back to the calling class. I cant do this with runnable, so have been reading up on Runnable with a Future. ot sure how this would be implemented. In my caling class I can make checks to see if the thread is complete (isdone() ?) and get any exceptions with get(), but in the class which implements the runnable not sure how I would set any exceptions that can be picked up by the calling class? Also would the calling class have to sit in a continuous while loop waiting for the isdone() to complete?
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The javadocs of the Future class have an example of how to return a String object from thread, but you use any other class instead of String.

As for handling exceptions, you should catch ExecutionException - that's what the JVM will throw if the Future class threw an exception.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic