wood burning stoves 2.0*
The moose likes Threads and Synchronization and the fly likes Executors in Threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Executors in Threads" Watch "Executors in Threads" New topic
Author

Executors in Threads

Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
Dear Sir,

I'm trying a code using Executors implementing the Callable interface to calculate sum of Fibonacci series. I cannot understand where the mistake is. Besides i don't seem to find any ease in using them against the Thread class. Using callable makes it more tough as we can't return strings while returning Integers robbing the code of displaying user friendly mesages to better understand the program execution

THe following code is not executing giving a java out of memory error.




can you please tell me what is wrong and why. I could display the fibonacci series by implementing the Runnable interface. can't we return the entire series using Callable.
Please tell me why.

with regards,
Komal
Jan Van den bosch
Greenhorn

Joined: Apr 17, 2008
Posts: 16
The problem is not the Callable. You're not decrementing count so the algorithm goes on forever -- or at least until you run out of memory. Why are you using two Lists, instance variables at that?
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

The OutOfMemoryError is happening because you are continuously adding new Integers to an ArrayList, and never reaching and end condition:

The loop here should end when count reaches n, but since you never change the value of count, the loop continuously adds new integers until there is no memory left. I suspect that your Runnable code either increments the count variable or hides the OutOfMemoryException.

Using callable makes it more tough as we can't return strings while returning Integers robbing the code of displaying user friendly mesages to better understand the program execution


You only have to return an Integer if you define the Callable as a Callable<Integer>. If you define it as a Callable<String> you can return a String. I would prefer the Callable<Integer> and do the formatting in the display portion of the code (the System.out.println(...) part). But that is just my 'separation of concerns' mind set.

I could display the fibonacci series by implementing the Runnable interface.

Which you could do with Callable as well. Implementing Runnable doesn't give you any abilities you don't have with a Callable. You just implement the same code to get the same results.

can't we return the entire series using Callable

Yes you can.


Steve
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
I'am able to return the values by returning list in the call() having cganged its return type to ArrayList. I had to remove the generic parameter from Callable which otherwise is creating problems in the main().
But I'am uable to return the sum in the main as Future<Integer> is not accepting any operands i.e. + nor am i able to cast it to integer.

I had modified the above code as follows
The call() method modified follows:



The main as follows:


How can i calculate the sum.

Thanks in advance

with regards
komal
Jan Van den bosch
Greenhorn

Joined: Apr 17, 2008
Posts: 16
Okay, it's called a Fibonacci series or a Fibonacci sequence; that should give you some clue about what to return from the method. You can't return the series as a single Integer value since it consists of multiple values, and you can't return multiple values from a method since that's simply not possible in Java. If you decide to return a List<Integer> from your call() method, your implements clause should reflect that; after all, that's the whole point of the type parameter: it buys you type safety.
Komal Amaresh
Ranch Hand

Joined: Oct 06, 2008
Posts: 67
ok Jan,

oops. sorry you must have mailed it while i was modifying the code..... for i was after being able to return the list. Now as i say in my modified code how can I return the sum. I dont think we can call two call() methods in the same code. one returning an Integer other an ArrayList as I have removed the generic type suffixed to the callable in the class decleration. as mentioned in my modified reply where in i want to series in the list as well as sum of the the values in the series, how can i do it.

Thanks in advance,

with regards,
komal
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21

Komal Amaresh wrote:ok Jan,

oops. sorry you must have mailed it while i was modifying the code..... for i was after being able to return the list. Now as i say in my modified code how can I return the sum. I dont think we can call two call() methods in the same code. one returning an Integer other an ArrayList as I have removed the generic type suffixed to the callable in the class decleration. as mentioned in my modified reply where in i want to series in the list as well as sum of the the values in the series, how can i do it.

Thanks in advance,

with regards,
komal


It sounds like you are trying to make one method - the call() method - do too much. Like was previously said, you can only return one Object from a method at a time. If you want to return two different things, it is an indicator that you want two different methods.

So the question you should ask yourself is:

"What should I return from the call()/get() method?" If you want to return the sequence, you should make it a Callable<ArrayList><Integer>>, have your call() return the list. If you want the call() method to return the sum, then make it a Callable<Integer> and return the sum. You should then have a separate method inside the ExeFicci class that returns whichever Object (the sequence or the sum) the call() method does not. You probably will want to make some fancy checks inside this method to make sure it can't get called prior to the completion of the call() method (fancy meaning a check on a thread-safe boolean to determine if the value should be returned). For example:

 
 
subject: Executors in Threads