Callable interface

Hi,

I have just spent 2 days trying to make sense out of the Callable interface and Future Object. I understand them in essence based on javadocs and online resources I found.

But, I still do not know how to achieve my requirement:

I have implemented Multithreading using Callable interface.

It returns an Object called ResultObject.

It can possibly throw an exception.

1.) I dont know how to access the returned object

2.) How to catch the exception.

thanks in advance

Vikas

[539 byte] By [arrowinthedusta] at [2007-10-3 7:50:14]
# 1

Generally a Callable is intended to be executed asynchronously on a different thread. If you want to have access to the result then the facility that is responsible for executing the Callable must provide some means of doing that. The easiest thing for you to do is simply to use a FutureTask which does just that. Wrap the Callable in a FutureTask and use that for the asynchronous execution. You can then use FutureTask to see if it's completed and retrieve the result, blocking if necessary.

Also, if you're using an ExecutorService you should just pass the Callable to the submit(Callable) method and use the returned Future to get the result.

kablaira at 2007-7-15 2:52:05 > top of Java-index,Java Essentials,Java Programming...