В чём может быть причина?
Код:
public static class CountUpRunnable implements Runnable{
//Add your code here - добавь код тут
private int countIndexUp = 1;
public int getCountIndexUp() {
return countIndexUp;
}
public void run() {
try { while (true) {
System.out.println(toString());
countIndexUp += 1;
if (countIndexUp > Solution.number) return;
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String toString() {
return Thread.currentThread().getName() + ": " + countIndexUp;
}
}
Вывод в консоль:
C:\Users\admin\.jdks\openjdk-15.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\lib\idea_rt.jar=56855:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.2.3\bin" -Dfile.encoding=UTF-8 -classpath D:\IDEA_project\JavaRushTasks\out\production\2.JavaCore com.javarush.task.task16.task1626.Solution
Увеличиваем: 1
Уменьшаем: 5
Уменьшаем: 4
Увеличиваем: 2
Уменьшаем: 3
Увеличиваем: 3
Уменьшаем: 2
Увеличиваем: 4
Увеличиваем: 5
Уменьшаем: 1
Process finished with exit code 0
Валидатор не принимает и пишет:
Метод run класса CountUpRunnable должен работать примерно 2,5 секунды.package com.javarush.task.task16.task1626;
/*
Создание по образцу
*/
public class Solution {
public static int number = 5;
public static void main(String[] args) {
new Thread(new CountdownRunnable(), "Уменьшаем").start();
new Thread(new CountUpRunnable(), "Увеличиваем").start();
}
public static class CountUpRunnable implements Runnable{
//Add your code here - добавь код тут
private int countIndexUp = 1;
public int getCountIndexUp() {
return countIndexUp;
}
public void run() {
try { while (true) {
System.out.println(toString());
countIndexUp += 1;
if (countIndexUp > Solution.number) return;
Thread.sleep(500);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public String toString() {
return Thread.currentThread().getName() + ": " + countIndexUp;
}
}
public static class CountdownRunnable implements Runnable {
private int countIndexDown = Solution.number;
public void run() {
try {
while (true) {
System.out.println(toString());
countIndexDown -= 1;
if (countIndexDown == 0) return;
Thread.sleep(500);
}
} catch (InterruptedException e) {
}
}
public String toString() {
return Thread.currentThread().getName() + ": " + countIndexDown;
}
}
}