面试官:你能说说生产者消费者的几种实现方式吗
作者:zgj12138
来源:https://juejin.cn/post/6844903486895865864
前言
生产者和消费者问题是线程模型中的经典问题:生产者和消费者在同一时间段内共用同一个存储空间,生产者往存储空间中添加产品,消费者从存储空间中取走产品,当存储空间为空时,消费者阻塞,当存储空间满时,生产者阻塞。
现在用四种方式来实现生产者消费者模型
wait()和notify()方法的实现
这也是最简单最基础的实现,缓冲区满和为空时都调用wait()方法等待,当生产者生产了一个产品或者消费者消费了一个产品之后会唤醒所有线程。
1 public class Test1 {
2 private static Integer count = 0;
3 private static final Integer FULL = 10;
4 private static String LOCK = "lock";
5
6 public static void main(String[] args) {
7 Test1 test1 = new Test1();
8 new Thread(test1.new Producer()).start();
9 new Thread(test1.new Consumer()).start();
10 new Thread(test1.new Producer()).start();
11 new Thread(test1.new Consumer()).start();
12 new Thread(test1.new Producer()).start();
13 new Thread(test1.new Consumer()).start();
14 new Thread(test1.new Producer()).start();
15 new Thread(test1.new Consumer()).start();
16 }
17 class Producer implements Runnable {
18 @Override
19 public void run() {
20 for (int i = 0; i < 10; i++) {
21 try {
22 Thread.sleep(3000);
23 } catch (Exception e) {
24 e.printStackTrace();
25 }
26 synchronized (LOCK) {
27 while (count == FULL) {
28 try {
29 LOCK.wait();
30 } catch (Exception e) {
31 e.printStackTrace();
32 }
33 }
34 count++;
35 System.out.println(Thread.currentThread().getName() + "生产者生产,目前总共有" + count);
36 LOCK.notifyAll();
37 }
38 }
39 }
40 }
41 class Consumer implements Runnable {
42 @Override
43 public void run() {
44 for (int i = 0; i < 10; i++) {
45 try {
46 Thread.sleep(3000);
47 } catch (InterruptedException e) {
48 e.printStackTrace();
49 }
50 synchronized (LOCK) {
51 while (count == 0) {
52 try {
53 LOCK.wait();
54 } catch (Exception e) {
55 }
56 }
57 count--;
58 System.out.println(Thread.currentThread().getName() + "消费者消费,目前总共有" + count);
59 LOCK.notifyAll();
60 }
61 }
62 }
63 }
64 }
结果
1Thread-0生产者生产,目前总共有1
2Thread-4生产者生产,目前总共有2
3Thread-3消费者消费,目前总共有1
4Thread-1消费者消费,目前总共有0
5Thread-2生产者生产,目前总共有1
6Thread-6生产者生产,目前总共有2
7Thread-7消费者消费,目前总共有1
8Thread-5消费者消费,目前总共有0
9Thread-0生产者生产,目前总共有1
10Thread-4生产者生产,目前总共有2
11Thread-3消费者消费,目前总共有1
12Thread-6生产者生产,目前总共有2
13Thread-1消费者消费,目前总共有1
14Thread-7消费者消费,目前总共有0
15Thread-2生产者生产,目前总共有1
16Thread-5消费者消费,目前总共有0
17Thread-0生产者生产,目前总共有1
18Thread-4生产者生产,目前总共有2
19Thread-3消费者消费,目前总共有1
20Thread-7消费者消费,目前总共有0
21Thread-6生产者生产,目前总共有1
22Thread-2生产者生产,目前总共有2
23Thread-1消费者消费,目前总共有1
24Thread-5消费者消费,目前总共有0
25Thread-0生产者生产,目前总共有1
26Thread-4生产者生产,目前总共有2
27Thread-3消费者消费,目前总共有1
28Thread-1消费者消费,目前总共有0
29Thread-6生产者生产,目前总共有1
30Thread-7消费者消费,目前总共有0
31Thread-2生产者生产,目前总共有1
可重入锁ReentrantLock的实现
java.util.concurrent.lock 中的 Lock
框架是锁定的一个抽象,通过对lock的lock()方法和unlock()方法实现了对锁的显示控制,而synchronize()则是对锁的隐性控制。
可重入锁,也叫做递归锁,指的是同一线程 外层函数获得锁之后
,内层递归函数仍然有获取该锁的代码,但不受影响,简单来说,该锁维护这一个与获取锁相关的计数器,如果拥有锁的某个线程再次得到锁,那么获取计数器就加1,函数调用结束计数器就减1,然后锁需要被释放两次才能获得真正释放。已经获取锁的线程进入其他需要相同锁的同步代码块不会被阻塞。
1import java.util.concurrent.locks.Condition;
2 import java.util.concurrent.locks.Lock;
3 import java.util.concurrent.locks.ReentrantLock;
4 /**
5 * 生产者和消费者,ReentrantLock的实现
6 *
7 * @author ZGJ
8 * @date 2017年6月22日
9 */
10 public class Test2 {
11 private static Integer count = 0;
12 private static final Integer FULL = 10;
13 //创建一个锁对象
14 private Lock lock = new ReentrantLock();
15 //创建两个条件变量,一个为缓冲区非满,一个为缓冲区非空
16 private final Condition notFull = lock.newCondition();
17 private final Condition notEmpty = lock.newCondition();
18 public static void main(String[] args) {
19 Test2 test2 = new Test2();
20 new Thread(test2.new Producer()).start();
21 new Thread(test2.new Consumer()).start();
22 new Thread(test2.new Producer()).start();
23 new Thread(test2.new Consumer()).start();
24 new Thread(test2.new Producer()).start();
25 new Thread(test2.new Consumer()).start();
26 new Thread(test2.new Producer()).start();
27 new Thread(test2.new Consumer()).start();
28 }
29 class Producer implements Runnable {
30 @Override
31 public void run() {
32 for (int i = 0; i < 10; i++) {
33 try {
34 Thread.sleep(3000);
35 } catch (Exception e) {
36 e.printStackTrace();
37 }
38 //获取锁
39 lock.lock();
40 try {
41 while (count == FULL) {
42 try {
43 notFull.await();
44 } catch (InterruptedException e) {
45 e.printStackTrace();
46 }
47 }
48 count++;
49 System.out.println(Thread.currentThread().getName()
50 + "生产者生产,目前总共有" + count);
51 //唤醒消费者
52 notEmpty.signal();
53 } finally {
54 //释放锁
55 lock.unlock();
56 }
57 }
58 }
59 }
60 class Consumer implements Runnable {
61 @Override
62 public void run() {
63 for (int i = 0; i < 10; i++) {
64 try {
65 Thread.sleep(3000);
66 } catch (InterruptedException e1) {
67 e1.printStackTrace();
68 }
69 lock.lock();
70 try {
71 while (count == 0) {
72 try {
73 notEmpty.await();
74 } catch (Exception e) {
75 e.printStackTrace();
76 }
77 }
78 count--;
79 System.out.println(Thread.currentThread().getName()
80 + "消费者消费,目前总共有" + count);
81 notFull.signal();
82 } finally {
83 lock.unlock();
84 }
85 }
86 }
87 }
88 }
阻塞队列BlockingQueue的实现
BlockingQueue即阻塞队列,从阻塞这个词可以看出,在某些情况下对阻塞队列的访问可能会造成阻塞。被阻塞的情况主要有如下两种:
当队列满了的时候进行入队列操作
当队列空了的时候进行出队列操作
因此,当一个线程对已经满了的阻塞队列进行入队操作时会阻塞,除非有另外一个线程进行了出队操作,当一个线程对一个空的阻塞队列进行出队操作时也会阻塞,除非有另外一个线程进行了入队操作。
从上可知,阻塞队列是线程安全的。
下面是BlockingQueue接口的一些方法:
| 操作 | 抛异常 | 特定值 | 阻塞 | 超时 |
|---|---|---|---|---|
| 插入 | add(o) | offer(o) | put(o) | offer(o, timeout, timeunit) |
| 移除 | remove(o) | poll(o) | take(o) | poll(timeout, timeunit) |
| 检查 | element(o) | peek(o) | none | none |
这四类方法分别对应的是:
1 . ThrowsException:如果操作不能马上进行,则抛出异常
2 . SpecialValue:如果操作不能马上进行,将会返回一个特殊的值,一般是true或者false
3 . Blocks:如果操作不能马上进行,操作会被阻塞
4 . TimesOut:如果操作不能马上进行,操作会被阻塞指定的时间,如果指定时间没执行,则返回一个特殊值,一般是true或者false
下面来看由阻塞队列实现的生产者消费者模型,这里我们使用take()和put()方法,这里生产者和生产者,消费者和消费者之间不存在同步,所以会出现连续生成和连续消费的现象
1import java.util.concurrent.ArrayBlockingQueue;
2 import java.util.concurrent.BlockingQueue;
3 /**
4 * 使用BlockingQueue实现生产者消费者模型
5 * @author ZGJ
6 * @date 2017年6月29日
7 */
8 public class Test3 {
9 private static Integer count = 0;
10 //创建一个阻塞队列
11 final BlockingQueue blockingQueue = new ArrayBlockingQueue<>(10);
12 public static void main(String[] args) {
13 Test3 test3 = new Test3();
14 new Thread(test3.new Producer()).start();
15 new Thread(test3.new Consumer()).start();
16 new Thread(test3.new Producer()).start();
17 new Thread(test3.new Consumer()).start();
18 new Thread(test3.new Producer()).start();
19 new Thread(test3.new Consumer()).start();
20 new Thread(test3.new Producer()).start();
21 new Thread(test3.new Consumer()).start();
22 }
23 class Producer implements Runnable {
24 @Override
25 public void run() {
26 for (int i = 0; i < 10; i++) {
27 try {
28 Thread.sleep(3000);
29 } catch (Exception e) {
30 e.printStackTrace();
31 }
32 try {
33 blockingQueue.put(1);
34 count++;
35 System.out.println(Thread.currentThread().getName()
36 + "生产者生产,目前总共有" + count);
37 } catch (InterruptedException e) {
38 e.printStackTrace();
39 }
40 }
41 }
42 }
43 class Consumer implements Runnable {
44 @Override
45 public void run() {
46 for (int i = 0; i < 10; i++) {
47 try {
48 Thread.sleep(3000);
49 } catch (InterruptedException e1) {
50 e1.printStackTrace();
51 }
52 try {
53 blockingQueue.take();
54 count--;
55 System.out.println(Thread.currentThread().getName()
56 + "消费者消费,目前总共有" + count);
57 } catch (InterruptedException e) {
58 e.printStackTrace();
59 }
60 }
61 }
62 }
63 }
信号量Semaphore的实现
Semaphore(信号量)是用来控制同时访问特定资源的线程数量,它通过协调各个线程,以保证合理的使用公共资源,在操作系统中是一个非常重要的问题,可以用来解决哲学家就餐问题。Java中的Semaphore维护了一个许可集,一开始先设定这个许可集的数量,可以使用acquire()方法获得一个许可,当许可不足时会被阻塞,release()添加一个许可。在下列代码中,还加入了另外一个mutex信号量,维护生产者消费者之间的同步关系,保证生产者和消费者之间的交替进行
1import java.util.concurrent.Semaphore;
2 /**
3 * 使用semaphore信号量实现
4 * @author ZGJ
5 * @date 2017年6月29日
6 */
7 public class Test4 {
8 private static Integer count = 0;
9 //创建三个信号量
10 final Semaphore notFull = new Semaphore(10);
11 final Semaphore notEmpty = new Semaphore(0);
12 final Semaphore mutex = new Semaphore(1);
13 public static void main(String[] args) {
14 Test4 test4 = new Test4();
15 new Thread(test4.new Producer()).start();
16 new Thread(test4.new Consumer()).start();
17 new Thread(test4.new Producer()).start();
18 new Thread(test4.new Consumer()).start();
19 new Thread(test4.new Producer()).start();
20 new Thread(test4.new Consumer()).start();
21 new Thread(test4.new Producer()).start();
22 new Thread(test4.new Consumer()).start();
23 }
24 class Producer implements Runnable {
25 @Override
26 public void run() {
27 for (int i = 0; i < 10; i++) {
28 try {
29 Thread.sleep(3000);
30 } catch (InterruptedException e) {
31 e.printStackTrace();
32 }
33 try {
34 notFull.acquire();
35 mutex.acquire();
36 count++;
37 System.out.println(Thread.currentThread().getName()
38 + "生产者生产,目前总共有" + count);
39 } catch (InterruptedException e) {
40 e.printStackTrace();
41 } finally {
42 mutex.release();
43 notEmpty.release();
44 }
45 }
46 }
47 }
48 class Consumer implements Runnable {
49 @Override
50 public void run() {
51 for (int i = 0; i < 10; i++) {
52 try {
53 Thread.sleep(3000);
54 } catch (InterruptedException e1) {
55 e1.printStackTrace();
56 }
57 try {
58 notEmpty.acquire();
59 mutex.acquire();
60 count--;
61 System.out.println(Thread.currentThread().getName()
62 + "消费者消费,目前总共有" + count);
63 } catch (InterruptedException e) {
64 e.printStackTrace();
65 } finally {
66 mutex.release();
67 notFull.release();
68 }
69 }
70 }
71 }
72 }
管道输入输出流PipedInputStream和PipedOutputStream实现
在java的io包下,PipedOutputStream和PipedInputStream分别是管道输出流和管道输入流。
它们的作用是让多线程可以通过管道进行线程间的通讯。在使用管道通信时,必须将PipedOutputStream和PipedInputStream配套使用。
使用方法:先创建一个管道输入流和管道输出流,然后将输入流和输出流进行连接,用生产者线程往管道输出流中写入数据,消费者在管道输入流中读取数据,这样就可以实现了不同线程间的相互通讯,但是这种方式在生产者和生产者、消费者和消费者之间不能保证同步,也就是说在一个生产者和一个消费者的情况下是可以生产者和消费者之间交替运行的,多个生成者和多个消费者者之间则不行
1/**
2 * 使用管道实现生产者消费者模型
3 * @author ZGJ
4 * @date 2017年6月30日
5 */
6 public class Test5 {
7 final PipedInputStream pis = new PipedInputStream();
8 final PipedOutputStream pos = new PipedOutputStream();
9 {
10 try {
11 pis.connect(pos);
12 } catch (IOException e) {
13 e.printStackTrace();
14 }
15 }
16 class Producer implements Runnable {
17 @Override
18 public void run() {
19 try {
20 while(true) {
21 Thread.sleep(1000);
22 int num = (int) (Math.random() * 255);
23 System.out.println(Thread.currentThread().getName() + "生产者生产了一个数字,该数字为: " + num);
24 pos.write(num);
25 pos.flush();
26 }
27 } catch (Exception e) {
28 e.printStackTrace();
29 } finally {
30 try {
31 pos.close();
32 pis.close();
33 } catch (IOException e) {
34 e.printStackTrace();
35 }
36 }
37 }
38 }
39 class Consumer implements Runnable {
40 @Override
41 public void run() {
42 try {
43 while(true) {
44 Thread.sleep(1000);
45 int num = pis.read();
46 System.out.println("消费者消费了一个数字,该数字为:" + num);
47 }
48 } catch (Exception e) {
49 e.printStackTrace();
50 } finally {
51 try {
52 pos.close();
53 pis.close();
54 } catch (IOException e) {
55 e.printStackTrace();
56 }
57 }
58 }
59 }
60 public static void main(String[] args) {
61 Test5 test5 = new Test5();
62 new Thread(test5.new Producer()).start();
63 new Thread(test5.new Consumer()).start();
64 }
65 }推荐阅读
android ViewPager 仿画廊/图书翻页 与 palette 使用
耗时一周,我解决了微信 Matrix 增量编译的 Bug,已提 PR
公众号徐公回复黑马,获取 Android 学习视频 公众号徐公回复徐公666,获取简历模板,教你如何优化简历,走近大厂 公众号徐公回复面试,可以获得面试常见算法,剑指 ofer 题解 公众号徐公回复马士兵,可以获得马士兵学习视频一份