package com.codegym.task.task16.task1614;
import java.util.ArrayList;
import java.util.List;
/*
Countdown
*/
public class Solution {
public static volatile List<String> list = new ArrayList<>(5);
static {
for (int i = 0; i < 5; i++) {
list.add("Line " + i);
}
}
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new Countdown(3), "Countdown");
t.start();
}
public static class Countdown implements Runnable {
private int countFrom;
public Countdown(int countFrom) {
this.countFrom = countFrom;
}
public void run() {
try {
while (countFrom > 0) {
printCountdown();
}
} catch (InterruptedException e) {
}
}
public void printCountdown() throws InterruptedException {
//write your code here
Thread.sleep(500);
while(true)
{
countFrom--;
System.out.println(Solution.list.get(countFrom));
if(this.countFrom==0) break;
}
}
}
}
Help please
Resolved
Comments (3)
- Popular
- New
- Old
You must be signed in to leave a comment
Guadalupe Gagnon
14 April 2019, 16:19solution
Your logic is a little jumbled.
#1a) line 34 already places the printCountdown method in a while loop, you do not need a loop inside the method.
#1b) line 34 already has a condition that breaks the loop when there are no more things to print, make sure you also remove the 'break' inside the method along with the loop it breaks.
#2) you want it to print to screen every half second, which you do accomplish, however your sleep should occur after the first print, not before.
+9
Satya Vath
16 April 2019, 12:38
Thanks For Your Support Gagnon
0
Mateo
1 July 2019, 09:26
Guadalupe is CodeGyms MVP!
+4