aspose file tools*
The moose likes Threads and Synchronization and the fly likes Recursive threads in threadpool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "Recursive threads in threadpool" Watch "Recursive threads in threadpool" New topic
Author

Recursive threads in threadpool

Sify Alto
Greenhorn

Joined: Mar 22, 2010
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

Joined: Sep 20, 2010
Posts: 3647
    
  16

Why would you do this with multiple threads? Is it for practice?
Sify Alto
Greenhorn

Joined: Mar 22, 2010
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.
John Pradeep.v
Ranch Hand

Joined: Jul 21, 2008
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

Joined: Sep 20, 2010
Posts: 3647
    
  16

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.

Don't lock on answer. Lock on this instead.
Sify Alto
Greenhorn

Joined: Mar 22, 2010
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

Joined: Mar 22, 2010
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.

Don't lock on answer. Lock on this instead.


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

Joined: Jan 28, 2003
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
Steve Luke
Bartender

Joined: Jan 28, 2003
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:



Boom - 5th generation requires 31 threads and your thread pool only has 30 threads in it. The work can't complete.
 
 
subject: Recursive threads in threadpool