I think i have to work with the interrupted Exception. I tried to put the output "Unterbrochen!" and delet the if statment in main there but it still does not work.
package de.codegym.task.task16.task1617;
/*
Countdown beim Rennen
*/
import jdk.nashorn.internal.ir.BlockStatement;
public class Solution {
public static volatile int numSeconds = 4;
public static boolean isCanceled = false;
public static void main(String[] args) throws InterruptedException {
RacingClock clock = new RacingClock();
Thread.sleep(3500);
isCanceled = true;
clock.interrupt();
}
public static class RacingClock extends Thread {
public RacingClock() {
start();
}
public void run() {
while(!isCanceled){
if(numSeconds == 0){
System.out.print("Los!");
isCanceled = true;
}else {
System.out.print(numSeconds + " ");
try {
sleep(1000);
numSeconds--;
} catch (InterruptedException e) {
System.out.println("Unterbrochen!");
}
}
}
}
}
}