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

fibonaccei with threads

subrahmanyam pvb
Greenhorn

Joined: Jan 02, 2006
Posts: 3
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.



balu------->>U.
Max Habibi
town drunk
( and author)
Sheriff

Joined: Jun 27, 2002
Posts: 4118
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 ]

Java Regular Expressions
Andreas Schaefer
Ranch Hand

Joined: Feb 13, 2006
Posts: 63
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

Joined: Feb 13, 2006
Posts: 63
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

Joined: Jan 02, 2006
Posts: 3
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: fibonaccei with threads