This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Using Callable in java 1.5  RSS feed

 
Vikas Bhardwaj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Scott Selikoff
author
Bartender
Posts: 4081
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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
 
Vikas Bhardwaj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
extremely Scott. I'll do the needful immediatly
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vikas Bhardwaj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
}
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Vikas Bhardwaj
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!