• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Threading Question

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Thread ends when its run() method ends.
 
john-paul York
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Spoor wrote:... There, I fixed it for you.


Doh! Thank you!
reply
    Bookmark Topic Watch Topic
  • New Topic