Skip to content

Commit 58dc2fc

Browse files
authored
stewards of the mysteries of God
Let a man so account of us, as of the ministers of Christ, and stewards of the mysteries of God. (1Cor 4:1)
1 parent 8c236f7 commit 58dc2fc

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
2+
//Let a man so account of us, as of the ministers of Christ, and stewards of the mysteries of God. (1Cor 4:1)
3+
4+
package com.javarush.task.task26.task2611;
5+
6+
import java.util.concurrent.ConcurrentHashMap;
7+
8+
public class Consumer implements Runnable {
9+
private ConcurrentHashMap<String, String> map;
10+
11+
public Consumer(ConcurrentHashMap<String, String> map) {
12+
this.map = map;
13+
}
14+
15+
public void run() {
16+
Thread currentThread = Thread.currentThread();
17+
while (!currentThread.isInterrupted()) {
18+
if (!map.isEmpty()) {
19+
for (String key : map.keySet()) {
20+
System.out.println(map.remove(key));
21+
}
22+
}
23+
}
24+
}
25+
}
26+
27+
/*
28+
Мир не меняется, меняемся мы
29+
Разберись с ConcurrentHashMap.
30+
В отдельном файле создайте класс Producer, который будет:
31+
1. каждые полсекунды добавлять в ConcurrentHashMap ключ и значение, где ключ — счетчик начиная с 1, значение — фраза: «Some text for i» , пример «Some text for 1«.
32+
2. при возникновении исключения выводить в консоль: «[TREAD_NAME] thread was terminated«, пример «[thread-1] thread was terminated«.
33+
34+
35+
Требования:
36+
1. Класс Producer должен быть создан в отдельном файле.
37+
2. Класс Producer должен реализовывать интерфейс Runnable.
38+
3. Класс Producer должен содержать приватное поле ConcurrentHashMap map.
39+
4. Класс Producer должен содержать конструктор с одним параметром, инициализирующий поле map.
40+
5. Метод run() класса Producer должен каждые полсекунды добавлять в ConcurrentHashMap ключ и значение согласно заданию.
41+
6. Метод run() класса Producer должен при возникновении исключения выводить в консоль "[TREAD_NAME] thread was terminated".
42+
43+
package com.javarush.task.task26.task2611;
44+
45+
import java.util.concurrent.ConcurrentHashMap;
46+
47+
public class Consumer implements Runnable {
48+
private ConcurrentHashMap<String, String> map;
49+
50+
public Consumer(ConcurrentHashMap<String, String> map) {
51+
this.map = map;
52+
}
53+
54+
public void run() {
55+
Thread currentThread = Thread.currentThread();
56+
while (!currentThread.isInterrupted()) {
57+
if (!map.isEmpty()) {
58+
for (String key : map.keySet()) {
59+
System.out.println(map.remove(key));
60+
}
61+
}
62+
}
63+
}
64+
}
65+
*/

0 commit comments

Comments
 (0)