File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threading Question

 
john-paul York
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Thread ends when its run() method ends.
 
john-paul York
Greenhorn
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:... There, I fixed it for you.

Doh! Thank you!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic