Works fine with numSeconds = 3; but otherwise it does the same thing. What am I missing? Also each time I run the code i get an IllegalThreadStateException :/ I'm completely lost and hopeless
package com.codegym.task.task16.task1617;
/*
Countdown at the races
*/
public class Solution {
public static volatile int numSeconds = 4;
public static void main(String[] args) throws InterruptedException {
RacingClock clock = new RacingClock();
clock.start();
Thread.sleep(3500);
clock.interrupt();
}
public static class RacingClock extends Thread {
public RacingClock() {
start();
}
public void run() {
Thread current = Thread.currentThread();
try {
while (numSeconds > 0) {
System.out.print(numSeconds + " ");
numSeconds--;
Thread.sleep(1000);
if (numSeconds < 1)
System.out.print("Go!");
else if (current.isInterrupted())
System.out.println("Interupted!");
}
} catch (InterruptedException e) {
}
}
}
}