Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!

Sify Alto
Greenhorn
Posts: 13
Hi friends,
I am calculating the 7th fibonacci number using threadpool. I am recursively creating threads to calculate. I am not able to figure out how to make the parent thread of each recursive call wait for their child threads to complete. Since I need the previous result of any computation for the next set of result,I am not sure how to achieve this in a threadpool since threads are being reused. Recursion being another factor to be taken into account. I have attached the code here.. During a few executions I managed to get the result upto the 4th fibonacci series . Any advice would be greatly appreciated. I have used to HashMap to keep track of the intermediate result being calculated by the threads.

Stephan van Hulst
Bartender
Posts: 5415
52
Why would you do this with multiple threads? Is it for practice?

Sify Alto
Greenhorn
Posts: 13
Yes, this is a sample program where I am trying to figure out how to work with recursive thread creation in a threadpool.

Ranch Hand
Posts: 59
are you trying it jus to experiment with threads? if not then using so many threads to find the nth fibonacci number is a no no.

The number of threads you use in any program and the synchronization between them should be choosen carefuly otherwise it will eat up memory and processing speed of your computer.... using so many threads along with recursive is again a wrong approach.

Binet's Formula finds out the nth fibonacci number with few multiplications and divisions.

Stephan van Hulst
Bartender
Posts: 5415
52
I haven't looked at your program in detail, but I've noticed something that might be a problem (I don't know if it is *the* problem, but it's definitely problematic).

You use answer as a lock. answer is an Integer, it's likely that it's going to change (as Integer's are immutable). So you are potentially locking on different objects.

Sify Alto
Greenhorn
Posts: 13
John Pradeep.v wrote:are you trying it jus to experiment with threads? if not then using so many threads to find the nth fibonacci number is a no no.

The number of threads you use in any program and the synchronization between them should be choosen carefuly otherwise it will eat up memory and processing speed of your computer.... using so many threads along with recursive is again a wrong approach.

Binet's Formula finds out the nth fibonacci number with few multiplications and divisions.

I am just experimenting with the code . Trying to figure out how to achieve some order in the execution of parent threads in a threadpool especially when creating threads recursively.

Sify Alto
Greenhorn
Posts: 13
Stephan van Hulst wrote:I haven't looked at your program in detail, but I've noticed something that might be a problem (I don't know if it is *the* problem, but it's definitely problematic).

You use answer as a lock. answer is an Integer, it's likely that it's going to change (as Integer's are immutable). So you are potentially locking on different objects.

Good catch !! Stephan .
I totally ignored that answer could be a different object every time a new value is calculated. I changed to lock to 'this' but still no luck. I guess there is something wrong in the way the the synchronization is happening in my code.

Steve Luke
Bartender
Posts: 4181
21
Each time through the fib sequence you create new threads, and take them out of the pool. Meanwhile the thread which is currently running must wait. So your problem probably comes from the fact that you have exhausted your ThreadPool - None of your threads are ending, but you are still trying to add more threads to do the work. I think you have to re-design your approach.

Steve Luke
Bartender
Posts: 4181
21
And actually, the math explains why you get the 4th Fib series, but can't get the 5th. Since each generation is producing 2 'child' threads, then the number of threads at each generation would be:

And since the previous generation can't complete until its children do, you for each generation, you need to add that number to the previous generations to get the total required threads: