The RaceClock object must decrease the numSeconds value by one every second (but it does!)
package com.codegym.task.task16.task1617;
/*
Countdown at the races
*/
public class Solution {
public static volatile int numSeconds = 3;
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() {
}
public void run() {
Thread current = currentThread();
while (numSeconds>0) {
System.out.print(numSeconds+" ");
try {
Thread.sleep(1000);
if (numSeconds==1 && !current.isInterrupted()) {
System.out.print("Go!");
break;
}
} catch (InterruptedException e) {
System.out.print("Interrupted!");
break;
}
numSeconds--;
}
}
}
}
/*1. Figure out what the program does.
2. Implement the run method so that each second
the count is displayed, separated by spaces and starting from numSeconds down to 1, and then "Go!" (see the examples).
3. If the thread runs for 3.5 seconds or more, interrupt it using the interrupt method and within the thread display "Interrupted!"
Example for numSeconds = 4 :
"4 3 2 1 Interrupted!"
4. If the thread runs less than 3.5 seconds, it should terminate itself.
Example for numSeconds = 3 :
"3 2 1 Go!"
PS: The sleep method throws an InterruptedException.*/