Sorry, your browser cannot access this site
This page requires browser support (enable) JavaScript
Learn more >

生产者消费者模式


生产者消费者模式

生产者和消费者指的是两个不同的线程类对象,操作同一资源的情况。

  • 生产者负责生产数据,消费者负责取走数据
  • 生产者每生产一组数据之后,互斥的放入缓冲区,消费者从缓冲区获取数据。
  • 上述同步的过程,需要对互斥数据进行同步处理,本下面的代码中通过synchronized进行代码同步。

结构图:

1596028363129

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package javatest.pattern;

import java.util.LinkedList;
import java.util.Random;

/**
* @description: 生产者消费者设计模式
* @modifyContent:
* @author: Maple Chan
* @date: 2020-07-29 20:31:06
* @version: 0.0.1
*/
public class ProducerAndConsumer {

public static void main(String[] args) {
new Producer().start();
new ConsumerA().start();
new ConsumerB().start();
}
}

class Producer extends Thread {
private void produce() {
Goods store = Goods.getInstance();

// do produce
long product = new Random().nextLong();
// put into store
synchronized (store) {
System.out.println("生产产品:" + product);
Goods.goodList.add(product);
}
}

@Override
public void run() {
int count = 3;
while (true) {
long sleepTime = new Random().nextInt(10) * 100;
try {
Thread.sleep(sleepTime);
} catch (Exception e) {
// TODO: handle exception
}
this.produce();
if (count-- < 0) {
break;
}
}

return;
}
}

class ConsumerA extends Thread {
private void consumer() {
Goods store = Goods.getInstance();

synchronized (store) {
Long product = Goods.goodList.getFirst();
Goods.goodList.remove(product);
System.out.println("消费产品:" + product);
}
}
@Override
public void run() {
while (true) {
long sleepTime = new Random().nextInt(10) * 100;

try {
Thread.sleep(sleepTime);
} catch (Exception e) {
// TODO: handle exception
}
if (Goods.goodList.size() > 0) {
this.consumer();
}
}
}
}

class ConsumerB extends Thread {
private void consumer() {
Goods store = Goods.getInstance();

synchronized (store) {
Long product = Goods.goodList.getFirst();
Goods.goodList.remove(product);
System.out.println("消费产品:" + product);
}
}
@Override
public void run() {
while (true) {
long sleepTime = new Random().nextInt(10) * 120;

try {
Thread.sleep(sleepTime);
} catch (Exception e) {
// TODO: handle exception
}
// 库存有东西才会进行消费
if (Goods.goodList.size() > 0) {
this.consumer();
}
}

}
}

class Goods {

public static LinkedList<Long> goodList;
private static Goods singletonGoods;
private Goods() {
}
static {
goodList = new LinkedList<>();
singletonGoods = new Goods();
}
/**
* 单例
*/
public static Goods getInstance() {
return singletonGoods;
}
}

输出如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*
生产产品:6805117464760743258
消费产品:6805117464760743258
生产产品:1599562522443449507
生产产品:1041166688112309013
消费产品:1599562522443449507
生产产品:124370676431480001
消费产品:1041166688112309013
生产产品:7414022159762949506
消费产品:124370676431480001
消费产品:7414022159762949506

代码中生产5个数据,之后消费将一直等待。
*/

评论