-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSolution.java
More file actions
123 lines (91 loc) · 3.59 KB
/
Solution.java
File metadata and controls
123 lines (91 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
//He will glorify me, for he will take from what is mine, and will declare it to you. (John 16:14)
package com.javarush.task.task16.task1614;
//He will glorify me, for he will take from what is mine, and will declare it to you. (John 16:14)
import java.util.ArrayList;
import java.util.List;
/*
Обратный отсчет
*/
public class Solution {
public static volatile List<String> list = new ArrayList<String>(5);
static {
for (int i = 0; i < 5; i++) {
list.add("Строка " + 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 {
countFrom --;//add your code here - добавь код тут
try{Thread.sleep(500);}catch (InterruptedException e) {e.printStackTrace();}
System.out.println(list.get(countFrom));
}
}
}
/*
Обратный отсчет
1. Разберись, что делает программа.
2. Реализуй логику метода printCountdown так, чтобы программа каждые полсекунды выводила объект из переменной list. Выводить нужно в обратном порядке — от переданного в Countdown индекса до нуля.
Пример:
Передан индекс 3
Пример вывода в консоль:
Строка 2
Строка 1
Строка 0
Требования:
1. Метод printCountdown должен работать примерно полсекунды.
2. Метод printCountdown должен уменьшать (декрементировать) значение переменной countFrom.
3. Метод printCountdown должен выводить элемент списка list с индексом равным новому значению countFrom.
4. Метод main должен создавать один объект типа Countdown.
5. Вывод программы должен соответствовать примеру из условия.
package com.javarush.task.task16.task1614;
import java.util.ArrayList;
import java.util.List;
/*
Обратный отсчет
*/
public class Solution {
public static volatile List<String> list = new ArrayList<String>(5);
static {
for (int i = 0; i < 5; i++) {
list.add("Строка " + 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 {
//add your code here - добавь код тут
}
}
}
*/