| Author |
Threading Question
|
john-paul York
Greenhorn
Joined: Feb 15, 2011
Posts: 16
|
|
I am creating a racing program to practice simple threads and this is what I have so far:
So this is simple enough, it prints out the name of the horse 50 times each randomly as the threads run at the same time. I am now looking to have a message printed out to say which horse won, this is where I run into issues. Is there a way maybe when a thread stops to print a message out? Can someone point me in the right direction?
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19230
|
|
|
A Thread ends when its run() method ends.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
john-paul York
Greenhorn
Joined: Feb 15, 2011
Posts: 16
|
|
Rob Spoor wrote:A Thread ends when its run() method ends.
am I correct in thinking the run() method is like the start() method? I was trying to figure out when the start menu finishs to print a message out....but can't seem to get it to work....I tried using a variable and then something like this:
if(horseOne.var != horseTwo.var)
{
print a message
}
but that did not work. My thought was if the two variables started the same and when one thread was done it could change its variable an they would be different. This if statement would then kick in and print the message...but as mentioned..that did not work.
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
john-paul York wrote:... am I correct in thinking the run() method is like the start() method? I was trying to figure out when the start menu finishs to print a message out....but can't seem to get it to work...
The start method calls the run method to execute as its own separate thread so that you have multi-threading. If you call the run method directly, then the code will just execute in the current thread, and you will not have multi-threading.
I think you're making this too complicated by setting flags (finished) that will need to be continuously checked and compared. To print a message when the run method is done, just put it at the end of the method body...
Edit: Corrected error found by Rob below.
|
"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Also, think about this...
If each RachHorse has its own "finished" variable, then for it to "know" whether it has finished first, it will need to check variables of all the other RachHorse instances (so it will need references to all other horses in the race). And if this process of checking other horse's variables is executing in multiple threads, then the process of checking could alter the results.
Currently, you're just using the Race class to house the main method. I suggest that you create an instance of Race that HAS multiple RaceHorses. Then the Race itself could have a single "winner" variable, and each horse is racing to change that variable first. (There would be some details to work out, but that's the idea.)
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19230
|
|
marc weber wrote:
john-paul York wrote:... am I correct in thinking the run() method is like the start() method? I was trying to figure out when the start menu finishs to print a message out....but can't seem to get it to work...
The start method calls the run method to execute as its own separate thread so that you have multi-threading. If you call the start run method directly, then the code will just execute in the current thread, and you will not have multi-threading.
There, I fixed it for you.
|
 |
marc weber
Sheriff
Joined: Aug 31, 2004
Posts: 11343
|
|
Rob Spoor wrote:... There, I fixed it for you.
Doh! Thank you!
|
 |
 |
|
|
subject: Threading Question
|
|
|