package com.javarush.task.task16.task1607;

import java.util.ArrayList;
import java.util.List;

/*
Horse Racing
*/

public class Solution {

    public static void main(String[] args) throws InterruptedException {
        List<Horse> horses = prepareHorsesAndStart(10);
        while (calculateHorsesFinished(horses) != horses.size()) {
        }


    }

    public static int calculateHorsesFinished(List<Horse> horses) throws InterruptedException {
        int finishedCount = 0;
        for (Horse horse: horses) {
            if (horse.isFinished() == true) {
                finishedCount++;
            }
            else {
                System.out.println("Waiting for " + horse.getName());
                horse.join();
            }
        }
      System.out.println("finishedCount = " + finishedCount);
        return finishedCount;
    }

    public static List<Horse> prepareHorsesAndStart(int horseCount) {
        List<Horse> horses = new ArrayList<>(horseCount);
        String number;
        for (int i = 1; i < horseCount + 1; i++) {
            number = i < 10 ? ("0" + i) : "" + i;
            horses.add(new Horse("Horse_" + number));
        }

        for (int i = 0; i < horseCount; i++) {
            horses.get(i).start();
        }
        return horses;
    }

    public static class Horse extends Thread {

        private boolean isFinished;

        public Horse(String name) {
            super(name);
        }

        public boolean isFinished() {
            return isFinished;
        }

        public void run() {
            String s = "";
            for (int i = 0; i < 1001; i++) {   // Delay
                s += "" + i;
                if (i == 1000) {
                    s = " has finished the race!";
                    System.out.println(getName() + s);
                    isFinished = true;
                }
            }
        }
    }
}
Вывод: Waiting for Horse_01 Horse_01 has finished the race! Waiting for Horse_02 Horse_02 has finished the race! Waiting for Horse_03 Horse_10 has finished the race! Horse_03 has finished the race! Waiting for Horse_04 Horse_04 has finished the race! Waiting for Horse_05 Horse_06 has finished the race! Horse_05 has finished the race! Waiting for Horse_07 Horse_08 has finished the race! Horse_07 has finished the race! Horse_09 has finished the race! finishedCount = 4 finishedCount = 10 Process finished with exit code 0 Подскажите, почему System.out.println(finishedCount) в методе calculateHorsesFinished в данном случае (строка 31) при исполнении программы выводит значение 2 раза? Первый раз - количество лошадей которое завершило гонку без необходимости ожидания, второй - всегда 10. Метод main использует значение 10, валидатор проглотил.