forked from DreamCats/java-notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathT15.java
More file actions
78 lines (65 loc) · 2.04 KB
/
T15.java
File metadata and controls
78 lines (65 loc) · 2.04 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.basic;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
/**
* @program JavaBooks
* @description: 写一个固定容量同步器,拥有put和get方法和getCount方法
* @author: mf
* @create: 2019/12/30 22:28
*/
/*
能够支持2个生产者线程以及10个消费者线程的阻塞调用
*/
public class T15<T> {
private final LinkedList<T> lists = new LinkedList<>();
private final int MAX = 10; // 最多10个元素
private int count = 0;
public synchronized void put(T t) {
while (lists.size() == MAX) { // 想想为什么用while而不是用if
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
lists.add(t);
count++;
this.notifyAll(); // 通知所有被wait挂起的线程
}
public synchronized T get() {
T t = null;
while (lists.size() == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
t = lists.removeFirst();
count--;
this.notifyAll(); // 通知所有被wait挂起的线程 用notify可能就死锁了。
return t;
}
public static void main(String[] args) {
T15<String> t15 = new T15<>();
// 启动消费者线程
for (int i = 0; i < 2; i++) {
new Thread(() -> {
for (int j = 0; j < 15; j++) System.out.println("消费者:" + t15.get());
}, "t15_" + i).start();
}
try {
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 启动生产者线程
for (int i = 0; i < 2; i++) {
new Thread(() -> {
for (int i1 = 0; i1 < 15; i1++) {
t15.put("生产者ID:" + Thread.currentThread().getName() + " 编号:" + i1);
}
}, "p" + i).start();
}
}
}