Win a copy of OCP Oracle Certified Professional Java SE 11 Developer Practice Tests this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
Sheriffs:
  • Devaka Cooray
  • paul wheaton
  • Mark Herschberg
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Frits Walraven
  • Jj Roberts
Bartenders:
  • Carey Brown
  • salvin francis
  • Piet Souris

Recursive threads in threadpool

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Saloon Keeper
Posts: 12720
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you do this with multiple threads? Is it for practice?
 
Sify Alto
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Saloon Keeper
Posts: 12720
277
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
PI day is 3.14 (march 14th) and is also einstein's birthday. And this is merely a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic