• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

fibonaccei with threads

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,

i am a novice in 'java' and new to this place. pls help me in solving this thing.

problem description:

there is a worm.
from the second minute of its birth, it gives another worm for every minute.
this goes with all the worms.

user input : no.of minutes.
desired output : no. of worms present.


here, i am trying simulate the above situation with multiple threads, wherein every thread, creates another thread from the second minute. and this goes till the given end.

but, my prog is not at all working and i can't figure out the problem.

PS: pls ignore the overhead of multiple threads. hope you will encourage my attempt to get the feel of 'threads' in java.

 
town drunk
( and author)
Posts: 4118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi subrahmanyam,


Welcome to the 'Ranch! The whole point of threading is concurrent work. That is, a bunch of 'guys' working on the same problem @ the same time. For example, imagine that you and a bunch of your friends are fixing your car. You 'guys' would divide the work between you: thus, one of you works on the tires, another on the engine, another changes the oil, etc.

If you're careful not to get into each other's way, you'll be done much faster then if you did all of those tasks one @ a time. This is the heart of Threading. Make sense?

Back to your problem. I'm not going to give you a specific answer, but I hope I can provide a general algorithm, and help you figure out how to code it.

It seems that you might choose to divide up the work along the lines of minutes. That is, spawn a thread for each minute. Each thread would calculate the amount of progress make in that minute.

Then, when all of the threads are done, you could add up the results.

On obvious challenge here is that you won't know what count to start with for a given thread. For example, what count should thread #3 start with, given that you can't assume that thread #1 & Thread #2 are done?

Here's a hint: think variables.

Ok, go code it up, and think aloud [here on the forum], with the problems you run into. I, or the other members here, will peek in on you from time to time and see if we can offer any advice.

-good luck!
M
[ March 23, 2006: Message edited by: Max Habibi ]
 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First you can use wait() in the run() method:
public void run() {
synchronized( this ) {
...
wait();
}
}

Secondly I think your program is not working because you need to wake up the waiting threds otherwise you only create new threads but they do not end. You can wake up the waiting threads by calling notify() or notifyAll() in the synchronized method or block. That means you need an exit condition telling the worm to end. In your case I think that is when no new worm is created (count reaches 0)

synchronized void progeny() {
try {
if(count>0) {
...
} else {
notify(); // Or notifyAll()
}
...
}

Please keep in mind that your sychronized method is not given you what you like because you are locking on 'this' and every worm is a differnt instance. You maybe want to use either this:

public void run() {
sychronized( this.getClass() ) {

or create an arbitrary static lock object:

private final static Object LOCK = new Object();
public void run() {
sychronized( LOCK ) {

Also remember that notify() or notifyAll() results that any thread may get the lock and not the parent (the one that created worm). In case you want the control then you need to join the parent thread with the child threads so that the parent is locked until all the children exist the run() method.

-Andy
 
Andreas Schaefer
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I just tried to make this work as far as I understood it:


Output:

count = 2
starting to wait root
prefix: 4.1.1.1 ends
prefix: 4.1.1 ends
prefix: 4.1.2.1 ends
prefix: 4.1.2 ends
prefix: 4.1 ends
prefix: 4.2.1.1 ends
prefix: 4.2.1 ends
prefix: 4.2.2.1 ends
prefix: 4.2.2 ends
prefix: 4.2 ends
prefix: 4.3.1.1 ends
prefix: 4.3.1 ends
prefix: 4.3.2.1 ends
prefix: 4.3.2 ends
prefix: 4.3 ends
prefix: 4 ends
ended to wait root


Hope that is somewhat what you wanted
 
subrahmanyam pvb
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all.
i didn't know 'javaranch' gives such a speedy response. liked this place.
thanks for putting all that stuff in order. that cleared the picture for me. but, i see this function 'activeCount()' there. so, if i can make all my threads 'wait()' until i count them, then i can 'notifyAll()' and stop them and the output would just be the 'count'. i am trying to improve on this. get you back soon.
i am now feeling confident that somebody is there who can guide me.

thanks to one & all.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic