java - Two threads finishing at the same time and accessing the same method -


i'm learning multithreading in java, i'm creating classic tortoise , hare race application.

for not familiar problem, basically, there's 2 racers:

  • a hare runs 100 meters in every loop has 90% chance of resting instead of running
  • a tortoise runs 10 meters in every loop, never rests

the first reach 1000 meters winner.

my code looks now:

main class:

public class thrace {      static thread hare      = new thread(new threadrunner("hare",       90, 100));     static thread tortoise  = new thread(new threadrunner("tortoise",   0,  10));      public static void main(string[] args) {         hare.start();         tortoise.start();     }      public static void finished(thread winner){         if (winner.getname().equals("hare")) tortoise.interrupt();         else if (winner.getname().equals("tortoise")) hare.interrupt();         }  } 

threadrunner class:

import java.util.random;  public class threadrunner implements runnable {      private string name;     private int rest, speed;      public string getracername() {         return name;     }      public threadrunner(string name, int rest, int speed) {         this.name = name;         this.rest = rest;         this.speed = speed;     }      @override     public void run() {          int meters = 0;         random rn = new random();          thread ct = thread.currentthread();         ct.setname(name);          while(!ct.isinterrupted()){             if(rn.nextint(100) + 1 > rest){                 meters += speed;                 system.out.println(this.name + ": " + meters);                             }              try {                 thread.sleep(100);             }catch (interruptedexception ex){                 return;                 //break;             }              if(meters >= 1000){                 system.out.println(this.name + ": finished!");                 thrace.finished(ct);                 return;             }          }     }  } 

when thread reaches 1000 meters calls finished method main class , stops other thread. code works fine, problem happens when there's tie (when hare , tortoise reach 1000 meters @ same tie), both runners declared winner , don't want happen.

for example, can set runners have 0% rest chance , same speed, both reach 1000 meters @ same time:

static thread hare      = new thread(new threadrunner("hare",       0,  10)); static thread tortoise  = new thread(new threadrunner("tortoise",   0,  10)); 

and output be:

(...) tortoise: 950 hare: 950 hare: 960 tortoise: 960 tortoise: 970 hare: 970 tortoise: 980 hare: 980 tortoise: 990 hare: 990 tortoise: 1000 hare: 1000 hare: finished! tortoise: finished! 

is there way me "lock" finished method, 1 thread declared winner?

if use synchronized identifier in front of finished method, first thread call lock until finished. since interrupting other thread, should work fine you.

synchronized public static void finished(thread winner){     if(winner.isinterrupted()) {         // sorry, other thread beat here         return;     }      if (winner.getname().equals("hare")) tortoise.interrupt();     else if (winner.getname().equals("tortoise")) hare.interrupt();     } 

as mentioned in comment, if other thread queued , waiting finished method, need add check see if interrupted before declaring winner.