Horse racing

  • 18
  • Locked
Programming is better than gambling... Still, horse races and other races perfectly illustrate multithreading! Figure out what the code does, and implement a method that counts the number of horses that have crossed the finish line. One more thing: you need to wait for the longshots to finish the race.
You can't complete this task, because you're not signed in.
Comments (12)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Jonaskinny Java Developer at Sandmedia
10 March 2022, 06:11
Not sure why validator is saying 'too much data in console' when I have functionally identical code, using object lock and wait() being the only difference, but I see now that's not the right way, so it's likely the validator is outputting weird errors since my output is the same Waiting for Horse_01 Horse_06 has finished the race! Horse_03 has finished the race! Horse_04 has finished the race! Horse_10 has finished the race! Horse_09 has finished the race! Horse_08 has finished the race! Horse_05 has finished the race! Horse_02 has finished the race! Horse_01 has finished the race! Waiting for Horse_07 Horse_07 has finished the race!
Justin Smith
Level 39 , Greenfield, USA, United States
3 September 2021, 21:25
I passed validation, but worth mentioning that this solution isn't perfect. The last time I ran it before verification, the last line was "Waiting for Horse_07" which appeared directly after "Horse_07 has finished the race!"
Davidh Dionicio
Level 17 , Lima, Peru
6 August 2021, 08:15
hola admin tengo un problema no me deja enviar la tarea lase tiempo de espera error de conexion
Level 24 , Wittenberg, Germany
5 June 2021, 07:23
task1607 (Horse racing) caveat: the validation code is broken; it seems that it doesn't recognise finish messages if they are printed before a wait message (which can easily happen since this is concurrent code with no interlocking, and the finished flag only gets set *after* the finish message is printed). The probability of getting failed erroneously is reduced significantly if you do the waiting right after determining that a horse has not yet finished. Of course, this skews the resulting picture since many other horses can finish while the loop waits for one of them...
Jurij Thmsn
Level 29 , Flensburg, Germany
16 March 2021, 14:39
The while loop in the main method confused me, apart from that I only needed multiple attempts because of a missing whitespace. make sure you calculateHorseFinished method returns "Waiting for " /*whitespace!*/ horse.getName();
Roman Grygorczuk
Level 19 , Cracow, Poland
4 January 2021, 20:56
Hi Admin! I believe that there is something wrong with tests because I passed it but the solution is wrong. Can you verify it? Thanks in advance! Roman
John Squirrels Website Admin at CodeGym
5 January 2021, 10:27
Please create your question in the Help section and send us the link.
John Squirrels Website Admin at CodeGym
5 January 2021, 10:28
Hi, Romanie
Roman Grygorczuk
Level 19 , Cracow, Poland
5 January 2021, 19:51
Hi John! Ahahah, nice to see you again XD All fine, I figure it out already, it was really easy fix. But thansk! ;)
Level 20 , London, United Kingdom
7 July 2020, 10:56
Hi It seems to work as requested...but it won't pass validation. I can't figure it out. \\\\any suggestions pls. Thanks
Muhammad Vahhaaj
Level 19 , Rawalpindi, Pakistan
15 July 2019, 10:38
Easy task, except i did not understand the use of while (calculateHorsesFinished(horses) != horses.size()) what is the loop used for?
Level 41
14 December 2020, 10:26
I believe the role of that loop is to create an infinite loop(or a long enough loop so it goes through each horse multiple times -like it happened to me-) that doesn't allow the program to finish if your int inside the calculateHorsesFinished does not reach the number of horses created