多生产消费者模式

真正的开发环境下,不可能只有两条线程在跑,但是也有特殊情况,就是只需要两条线程来处理。
比如一个线程生产,一个线程消费。
这是一种线程协作,这种情场景下,生产者消费者会操作同一个共享变量。
看到这里的小伙伴应该是对线程的基本用法有一定了解,这里就直接开始明确几个概念

生产者

生产数据的线程,比如产生订单

消费者

处理生产者产生的订单

实际环境中,生产数据的速度要大于消费的速度,这个现象在很多场景中都存在。

共享变量

会被多个线程共同访问的变量

生产者、消费者模式本质是,通过严格控制两个线程的交替执行,来实现一个生产、一个消费的模式,数据存储在共享变量中。

可以再扩展一下,比如常用的MQ,也是一种生产者消费者模式,Producer 生产消费,Consumer消费消息。

主要资源

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
/**
* @author liukai
* @since 2017/2/18.
*/
public class Resource {
private boolean flag;
private volatile int count = 0;

public synchronized void set() {
while (flag) { //注意,这里同样是判断标识位,但是这里和之前笔记不是的地方是,这里用的是while,而不是if。
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "-->生产" + count++);
this.flag = true;
this.notifyAll();
}

public synchronized void out() {
while (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "-->消费" + count++);
this.flag = false;
this.notifyAll();
}
}

生产者类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Producer implements Runnable {
private Resource res;

public Producer(Resource res) {
this.res = res;
}

@Override
public void run() {
while (true) {
res.set();
}
}
}

消费者类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Consumer implements Runnable {
Resource res;

public Consumer(Resource res) {
this.res = res;
}

@Override
public void run() {
while (true) {
res.out();
}
}
}

测试类

1
2
3
4
5
6
7
8
9
10
11
12
13
class Test {
public static void main(String[] args) {
Resource r = new Resource();
Producer in = new Producer(r);
Consumer out = new Consumer(r);

Thread t1 = new Thread(in);
Thread t2 = new Thread(out);

t1.start();
t2.start();
}
}

打印结果

1
2
3
4
5
6
7
Thread-0-->生产470
Thread-1-->消费471
Thread-0-->生产472
Thread-1-->消费473
Thread-0-->生产474
Thread-1-->消费475
Thread-0-->生产476