Meaningless Drivel is fun!*
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
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: 41101
    
  45
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
 
Consider Paul's rocket mass heater.
 
subject: Exceptions from Runnable
 
Similar Threads
RMI + Thread: perform searching on different worker PCs
Java threads
Threads, and the UI question.
Which type of implementation, extends Thread or implements Runnable. ?
Instantiating a Thread page no 679