-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.java
34 lines (29 loc) · 1.04 KB
/
Main.java
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
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;
public class Main {
public static void main(String[] args) {
BoundedBuffer<Integer> boundedBuffer = new BoundedBuffer<>(5);
Runnable producingTask = () -> Stream.iterate(0, i -> i + 1).forEach(i -> {
try {
boundedBuffer.put(i);
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
});
Thread producingThread = new Thread(producingTask);
Runnable consumingTask = () -> {
try {
while (!Thread.currentThread().isInterrupted()) {
boundedBuffer.take();
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
};
Thread consumingThread = new Thread(consumingTask);
producingThread.start();
consumingThread.start();
}
}