-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProducer.java
More file actions
64 lines (50 loc) · 2.14 KB
/
Producer.java
File metadata and controls
64 lines (50 loc) · 2.14 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
//Therefore let no man glory in men. For all things are yours; (1Cor 3:21)
package com.javarush.task.task26.task2610;
import java.util.concurrent.BlockingQueue;
public class Producer implements Runnable {
private BlockingQueue queue;
public Producer(BlockingQueue queue) {
this.queue = queue;
}
public void run() {
try {
int i = 0;
while (true) {
queue.put(String.valueOf(i++));
Thread.sleep(300);
}
} catch (InterruptedException e) {
System.out.println(String.format("[%s] thread was terminated", Thread.currentThread().getName()));
}
}
}
/*
Мир скучен для скучных людей
Разберись с BlockingQueue.
По образу и подобию класса Producer создай класс Consumer, который будет выводить данные из BlockingQueue в консоль.
Требования:
1. Класс Consumer должен быть создан в отдельном файле.
2. Класс Consumer должен реализовывать интерфейс Runnable.
3. Класс Consumer должен содержать приватное поле BlockingQueue queue.
4. Класс Consumer должен содержать конструктор с одним параметром, инициализирующий поле queue.
5. Метод run() класса Consumer должен постоянно выводить на экран значения из очереди.
package com.javarush.task.task26.task2610;
import java.util.concurrent.BlockingQueue;
public class Producer implements Runnable {
private BlockingQueue queue;
public Producer(BlockingQueue queue) {
this.queue = queue;
}
public void run() {
try {
int i = 0;
while (true) {
queue.put(String.valueOf(i++));
Thread.sleep(300);
}
} catch (InterruptedException e) {
System.out.println(String.format("[%s] thread was terminated", Thread.currentThread().getName()));
}
}
}
*/