aspose file tools
The moose likes Threads and Synchronization and the fly likes Using Callable in java 1.5 Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "Using Callable in java 1.5" Watch "Using Callable in java 1.5" New topic
Author

Using Callable in java 1.5

Vikas Bhardwaj
Greenhorn

Joined: Oct 20, 2006
Posts: 4
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
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3652

"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


My Blog: Down Home Country Coding with Scott Selikoff
Vikas Bhardwaj
Greenhorn

Joined: Oct 20, 2006
Posts: 4
extremely Scott. I'll do the needful immediatly
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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?


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Vikas Bhardwaj
Greenhorn

Joined: Oct 20, 2006
Posts: 4
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Oct 20, 2006
Posts: 4
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 agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Using Callable in java 1.5
 
Similar Threads
Question in Callable interface
kicking off independent task threads in parallel
Thread to return a value
Number of working threads using java executor framework
How can a checked exception be thrown from run method of a thread ?