1)Берем первый объект списка horses
2)проверяем финишировал ли он , если нет , то вызываем метод h.join();
3) ждем , пока он закончит.
Вопрос: Почему до завершения работы первого объекта в других нитях работают остальные объекты списка ?
Ведь до следующего цикла for должен был закончить работу метод join первого объекта .
public static int calculateHorsesFinished(List<Horse> horses) throws InterruptedException {
int countFinished = 0;
//add your implementation here - добавь свою реалзацию тут
for (Horse h : horses) {
if (!h.isFinished()) {
System.out.println("Waiting for " + h.getName());
h.join();
} else countFinished++;
}
return countFinished;
}
в результате :
Waiting for Horse_01
Horse_02 has finished the race!
Horse_01 has finished the race!
Waiting for Horse_03
Horse_04 has finished the race!
Horse_03 has finished the race!
Horse_10 has finished the race!
Waiting for Horse_05
Horse_08 has finished the race!
Horse_07 has finished the race!
Horse_06 has finished the race!
Horse_09 has finished the race!
Horse_05 has finished the race!