Forums Register Login

Using Callable in java 1.5

+Pie Number of slices to send: Send
Hi,
I need my Threads to return objects and throw Exceptions.
I am using java 1.5 Callable interface.

I have tried using the Future Object in Java 1.5.
Cant get it using the get() function of.
My program just hangs when it reaches the .get function of the class.
I am writing excrepts of my programs to make my problem clear.

Would really appriciate if I could get some explanation as I have not been able to understand clearly how Callable and Future work in Java 1.5

My Code:

private ExecutorService exServ = Executors.newFixedThreadPool(MAX_THREADS);
Future<resultData.B1Result> b1Result = exServ.submit(this);

public resultData.B1Result call() throws DryRunException
{
/*
might throw the DryRunException or
return b1Result;
*/
}

I have tried the statement:
b1Result.get();
but the software just hangs there.

Any help would be appriciated.

thanks in advance
Arrow
+Pie Number of slices to send: Send
"arrow dust",

There aren't many rules that you need to worry about here on the Ranch, but one that we take very seriously regards the use of proper names. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.

In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious.

Thanks!
Scott
+Pie Number of slices to send: Send
extremely Scott. I'll do the needful immediatly
+Pie Number of slices to send: Send
 

Originally posted by arrow dust:

I have tried the statement:
b1Result.get();
but the software just hangs there.



That would indicate that the Runnable doesn't finish. Can you show us the run() method?
+Pie Number of slices to send: Send
There is no run()just call().
My class implements Callable instead of Runnable.
I am pretty sure that that the Threads finishes its tasks.

The problem is(I think) related to new Java 1.5 object called Future.

I am sorry, I left out the class definition out of the above code:

public class ThreadCreator implements Callable <resultData.B1Result>
{
// the code given above...
}
+Pie Number of slices to send: Send
 

Originally posted by Vikas Bhardwaj:
There is no run()just call().
My class implements Callable instead of Runnable.
I am pretty sure that that the Threads finishes its tasks.

The problem is(I think) related to new Java 1.5 object called Future.



Future.get() waits for the Callable to finish. As soon as the Callable finished, it should return.

How sure is "pretty sure"? How did you check it?
+Pie Number of slices to send: Send
I put up a System.out.println() statement just before the return statement in thread.
The print statement checks the contents of object to return. it is created.
The very next statement is return that object.

Therefore, I am sure that the Thread is returning the values.
I don't like that guy. The tiny ad agrees with me.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1314 times.
Similar Threads
How can a checked exception be thrown from run method of a thread ?
Thread to return a value
Question in Callable interface
Number of working threads using java executor framework
kicking off independent task threads in parallel
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 09:53:08.