-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharrbloque.java
46 lines (43 loc) · 1.46 KB
/
arrbloque.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
35
36
37
38
39
40
41
42
43
44
45
46
import java.util.concurrent.ArrayBlockingQueue;
public class arrbloque {
public static void main(String[] args) throws InterruptedException {
ArrayBlockingQueue<Integer> queue = new ArrayBlockingQueue<>(3);
Runnable provider = () -> {
for (int i = 1; i<6; i++) {
try {
queue.put(i);
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
System.out.println("provider interrupted by smth");
}
System.out.println("provider "+queue);
}
};
Runnable consumer = () -> {
for (int i = 1; i<6; i++) {
try {
Thread.sleep(1500);
} catch (InterruptedException e) {
System.out.println("consumer interrupted by smth");
}
try {
queue.take();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("consumer "+queue);
}
};
Thread pt = new Thread(provider);
Thread ct = new Thread(consumer);
pt.start();
ct.start();
pt.join();
ct.join();
System.out.println(queue);
}
}