public class Solution {
public static volatile int numSeconds = 3;
public static void main(String[] args) throws InterruptedException {
RacingClock clock = new RacingClock();
//write your code here
Thread.sleep(3500);
clock.interrupt();
}
public static class RacingClock extends Thread {
public RacingClock() {
start();
}
public void run() {
//write your code here
try{
boolean isLessThanThree = (numSeconds <= 3);
while (numSeconds > 0){
System.out.print(numSeconds + " ");
Thread.sleep(1000);
numSeconds--;
}
if (isLessThanThree){
System.out.print("Go!");
}
}
catch (InterruptedException e) {
System.out.print("Interrupted!");
}
}
}
}
Why do I need to add "Thread.sleep(3500)" in the main method?
Resolved
Comments (1)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
28 February 2020, 14:42solution
the task specifically says:
3. If the thread runs for 3.5 seconds or more, interrupt it using the interrupt method and within the thread display "Interrupted!"
The sleep method uses milliseconds, so 3500 milliseconds equals 3.5 seconds.
+2