hi, i have this program that creates several threads. it is a horse racing program and every new race i create a new array of threads.
i tried creating the threads in the constructor for this class and just calling start each time, but that didnt work. is there a way to reuse the threads i already have? or is creating new threads each time like im doing the way its done?
Instead you should extend Thread and create a class that has a few methods. xyz.accept(Horse horse); xyz.go(); that way, when the race is over, you can reuse the threads by simple putting a new horse in them and telling them to go again. your run method should have a while loop that checks for new hourses. then when it receives the go message, it should run with the new horse.
im not sure i understand. maybe if i post some more of the code. i already turned in the assignment, but i didnt like the way i created new threads each time, so i want to understand how i could have done it differently. heck, its not that big, i post it all: Horse class
The point is, you can make a thread do work then wait on more work. you just have to keep a pointer to the thread around so you can insert more work when you want it to do more. here is some example code
here you add a few other methods. those methods will change the serverMode variable so the server can do what you want it to do. For instance, one method can add more data, set serverMode=RUN, then call notify() to wake the server up. this example is more complicated than you probably need.
hmmm...if i could see how to change my program maybe i would understand better...oh well, my code works. ill get 100% on that lab...just rubs me the wrong way doing it the way i did if there is a better way :roll:
ok, i know it might have sounded like i was asking for help on homework, but, like i said, the assignment was turned in before i even asked this question. i got 100% on it the way it was, and i got an A out of the course, so if anyone could please edit my code to show me how i could have reused the threads i had instead of creating new ones for each race, i would appreciated it a lot.
As far as I know, you cannot call 'start()' twice for a Thread. A Thread is managed by the OS. i.e. it get's a process-number. When it is finished, it's whiped out, and the OS may use the process-number for something else. Therefore the only way to reuse the thread would be not to stop it, but keep it running as CL Gilbert posts.
Yes, I think what you did was fine. Reusing threads becomes interesting when you have thousands of tasks that don't run all at once, like a web server handling each request on a thread. There is overhead in creating so many threads and danger in running too many at once. Thread pools are a solution to both risks. One of the replies above showed a thread that could be in several different states - running, waiting, etc. You could probably apply that to your horse race as an exercise. Another approach is to put the code that does the actual work in a "command" class. A command usually has a simple method signature like execute() that does the work. It's not a thread or runnable itself, but you get a thread to execute it. So the thread class does something like:
We can start up 5 threads, and they'll execute up to 5 commands at once. But what if the queue goes empty when no requests (think web server again) are coming in? We use a "blocking queue" with a get() method that will wait for a command to arrive and give it to one of the blocked threads. Google for Jakarta Commons Thread Pool for a wonderfully simple example. Cool stuff, great geek fun, but doesn't apply very well to your program. [ April 07, 2004: Message edited by: Stan James ]
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
thanks Stan, that is more or less the information i was looking for. my concern was that i might have been being wasteful of resources for no reason. it now seems that, concidering how few threads are involved, the extra effort (and probably extra code) involved in trying to reuse the threads would be the waste of resources instead. if i get real bored i might try it just for the learning experience though.