-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessor2.java
More file actions
39 lines (34 loc) · 856 Bytes
/
Copy pathProcessor2.java
File metadata and controls
39 lines (34 loc) · 856 Bytes
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
package examples5;
import java.util.LinkedList;
import java.util.Random;
public class Processor2 {
private LinkedList<Integer> list = new LinkedList<>();
private final static int LIMIT = 10;
private Object lock = new Object();
public void produce() throws InterruptedException {
int value = 0;
while (true) {
synchronized (lock) {
if (list.size() == LIMIT) {
lock.wait();
}
list.add(value++);
lock.notify();
}
}
}
public void consume() throws InterruptedException {
while (true) {
synchronized (lock) {
if (list.size() == 0) {
lock.wait();
}
System.out.print("List size: " + list.size());
int value = list.removeFirst();
System.out.println("; value is: " + value);
lock.notify();
}
Thread.sleep(new Random().nextInt(1000));
}
}
}