Java锁机制实现生产者消费者模型

简单记录一下,锁机制实现的P-C模型,原理看一遍代码就清楚了。

说句实话,

我们和大师的区别就是,编程大师们在没有参考的情况下能凭空构思出自己的解决方案,

并且还很完美,而我们只能在看完前辈的解决方案后才能恍然大悟,原来可以这么干,

第一个踩出路的人真的很厉害,我等凡人自愧不如

哈哈哈,废话真多。

这里使用Java内置锁synchronized实现,

主要就是利用wait()/notify()实现取/放两个操作的相互等待和通知,

容器为空,就wait阻塞住等待有元素加入时通知唤醒,队列满时也是如此。

用JDK提供的Lock锁也同样可以实现,步骤都差不多,就换一套API罢了。

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
import java.util.LinkedList;

class BlockingContainer<T> {
private Object notFullLock = new Object();
private Object notNullLock = new Object();
private int capacity;
private LinkedList<T> container = new LinkedList<T>();

public BlockingContainer(int capacity) {
this.capacity = capacity;
}

public T get() {
while (container.size() == 0) {
synchronized (notNullLock) {
try {
notNullLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

T data = container.poll();
--capacity;
synchronized (notFullLock) {
notFullLock.notify();
}
return data;
}

public boolean set(T value) {
while (container.size() == capacity) {
synchronized (notFullLock) {
try {
notFullLock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
boolean result = container.add(value);
++capacity;
synchronized (notNullLock) {
notNullLock.notify();
}
return result;
}
}

public class Main {

public static void main(String[] args) {
BlockingContainer<String> bc = new BlockingContainer<>(5);
new Thread(() -> {
System.out.println(Thread.currentThread().getName() + "尝试获取");
System.out.println(Thread.currentThread().getName() + "获取了一个元素:" + bc.get());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "设置了一个元素,状态:" + bc.set("test1"));
}).start();
new Thread(() -> {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "设置了一个元素,状态:" + bc.set("test2"));
System.out.println(Thread.currentThread().getName() + "尝试获取");
System.out.println(Thread.currentThread().getName() + "获取了一个元素:" + bc.get());
}).start();
}
}

输出:

Thread-0尝试获取
Thread-1设置了一个元素,状态:true
Thread-1尝试获取
Thread-0获取了一个元素:test2
Thread-0设置了一个元素,状态:true
Thread-1获取了一个元素:test1

文章作者: Shawn Qin
文章链接: https://qinshuang1998.github.io/2019/05/12/java-product-consumer/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 Shawn's Blog