Advertisement
jaVer404

level16.lesson07.task04

Sep 10th, 2015
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.76 KB | None | 0 0
  1. package com.javarush.test.level16.lesson07.task04;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /* Обратный отсчет
  7. 1. Разберись, что делает программа.
  8. 2. Реализуй логику метода printCountdown так, чтобы каждые полсекунды выводился объект из
  9. переменной list в обратном порядке - от переданного индекса до нуля.
  10. Пример: Передан индекс 3
  11. Пример вывода в консоль:
  12. Строка 2
  13. Строка 1
  14. Строка 0
  15. */
  16.  
  17. public class Solution {
  18.     public static volatile List<String> list = new ArrayList<String>(5);
  19.  
  20.     static {
  21.         for (int i = 0; i < 5; i++) {
  22.             list.add("Строка " + i);
  23.         }
  24.     }
  25.  
  26.     public static void main(String[] args) throws InterruptedException {
  27.         Thread t = new Thread(new Countdown(3));
  28.         t.start();
  29.     }
  30.  
  31.     public static class Countdown implements Runnable {
  32.         private int countFrom;
  33.  
  34.         public Countdown(int countFrom) {
  35.             this.countFrom = countFrom;
  36.         }
  37.  
  38.         public void run() {
  39.             try {
  40.                 while (countFrom > 0) {
  41.                     printCountdown();
  42.                 }
  43.             } catch (InterruptedException e) {
  44.             }
  45.         }
  46.  
  47.         public void printCountdown() throws InterruptedException {
  48.             //add your code here - добавь код тут
  49.             int tempCountFrom = this.countFrom-1;
  50.             for (int i = tempCountFrom; i >= 0; i--) {
  51.                 System.out.println(list.get(i));
  52.                 this.countFrom--;
  53.                 Thread.sleep(500);
  54.             }
  55.         }
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement