package com.codegym.task.task16.task1617;
/*
Countdown at the races
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!"
*/
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
}
public static class RacingClock extends Thread {
public RacingClock() {
start();
}
public void run() {
//write your code here
while(numSeconds >= 1) {
System.out.print(numSeconds + " ");
numSeconds -= 1;
}
if (isInterrupted()) {
System.out.print("Interrupted!");
} else {
System.out.print("Go!");
}
}
}
}
How can we determine time here?
Under discussion
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Learner
15 October 2018, 06:34
The answer to your question is given in the task example itself.
Example for numSeconds = 4 :
"4 3 2 1 Interrupted!"
So, you have to check whether numSeconds = 4 or less than 4. If 4 or more than 4 then, print interrupted. If less than 4 then print Go.
0
Baljinder Singh
15 October 2018, 15:59
I did it like this and it works, is it ok?
0
Learner
16 October 2018, 05:25
Your code is throwing UnhandledException.
Are you sure your code works?
0