Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Executors in Threads

 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Komal Amaresh
Ranch Hand
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic