wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Exceptions from Runnable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Exceptions from Runnable" Watch "Exceptions from Runnable" New topic
Author

Exceptions from Runnable

Dave Barnes
Greenhorn

Joined: Jun 10, 2013
Posts: 5
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
Marshal

Joined: Mar 22, 2005
Posts: 41591
    
  55
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.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions from Runnable