1

Please help me with my code. I'm making a race using There are 2 points in the Race starting point and the finish line. All horses started at the gate and proceed to the gate. The race will only starts once all horses reached the gate. The one who finished first wins the race.

javako
  • 447
  • 1
  • 4
  • 5

1 Answers1

1

The mistake you have is the number you provide here

gate= new CyclicBarrier(numHorses);

What is numHorses ?

It's the number of all horses but here :

list.removeIf(...);

You are removing the horses under 18, pretend we had 5 horses total, numHorses==5 is true, two of these are under 18 so we're left with 3.

You're creating three threads but the barrier is set on 5, the barrier will never get passed because it's waiting for 5 threads when there are only three.

The solution is to make addHorsesToList add horses without the barrier(so you'll need a Horse constructor without barrier too), then you move gate = new CyclicBarrier to after the list.removeIf and change numHorses to list.size().

After that you assign the barrier to the gate field of every horse object you have in list.

Do like this :

int canJoin=0;
    while(canJoin<2){
        list= new ArrayList<Horse>();
        numHorses=checkNumHorses();
        addHorsesToList();
        printHorses();
        canJoin=countJoinAge();
        }   

   list.removeIf(p -> p.Age().equals(18)); 
   int numHealthyHorses=list.size();
   gate=new CyclicBarrier(numHealthyHorses);  
   System.out.println("Horses who can join");

   Thread[] threads = new Thread[numHealthyHorses];
   for(Horse horse: list){     //create thread for each Horse
       horse.setGateBarrier(gate); //setter method, needs to be added to Horse class
       threads[numThread]= new Thread(horse);           
       }

   for(int i=0;i<numHealthyHorses;i++){     //start the thread for each horse
       threads[i].start();
       }}
niceman
  • 2,653
  • 29
  • 57
  • I did what you told but I'm getting Nullpointer exception before await();. – javako Jul 02 '16 at 15:54
  • what about `list` ? does every horse's `gate` points to the `gate` variable in HorseRaceMain before starting the threads ? – niceman Jul 02 '16 at 16:08
  • I tried to print out the horse list before and after the filtered list.stream. all horses have a null value for gate. What would be the problem? – javako Jul 02 '16 at 16:14
  • thanks niceman it work!! now I have another question to ask. how can I compare the distance travelled of each horse/thread. The reason for comparing is to know which is the trailing horse. Once I know which one is the trailing horse I will increase its speed like a boost. – javako Jul 02 '16 at 23:13
  • @javako you have this line : `int hop=hop();`, you can add a `hops` field in class Horse and after the previous line you put `hops+=hop`, you can compare on hops field as the distance travelled – niceman Jul 04 '16 at 13:23
  • yup I know I can access the distancetravelled from the variable distancetravelled or the hop method but what I want to know is how can I compare the distancetravelled between threads. Can you give me sample code to compare? – javako Jul 04 '16 at 22:13
  • Horse is a class, `distancetravelled` is a field, comparing them should be `h1.getDistanceTravelled()==h2.getDistanceTravelled()` or >=,>,etc. Anyway it depends on where you want to compare and what you intend to do – niceman Jul 04 '16 at 22:23
  • now my question is how can I compare each horse to other horses. example I want to compare 1st Horse distancetravelled to other horses then compare 2nd Horse distancetravelled to other horses and so on . please help me. – javako Jul 05 '16 at 02:43
  • I want to compare the distance every changes in distancetravelled of horses – javako Jul 05 '16 at 02:44
  • @javako hmmm if you ask a separate question on SO I would answer, the answer simply can't fit in comments – niceman Jul 05 '16 at 13:14