java并发编程生产者消费者模式

水深无声 2022-11-20 00:59 257阅读 0赞

为什么要使用生产者和消费者模式

由于生产与消费是两块功能,所以很难保证两块处理速度完全一致,为了解决这种生产消费能力不均衡的问题, 所以便有了生产者和消费者模式。

什么是生产者消费者模式

生产者和消费者彼此之间不直接通讯,而通过阻塞队列来进行通讯

实现方式一使用队列:

使用队列,生产者与消费之间通过队列相连,生产者将生产资料放入队列,消费者从队列里面取资源消费。

  1. 生产者消费者的示例代码:
  2. 生产者:
  3. import java.util.concurrent.BlockingQueue;
  4. public class Producer implements Runnable {
  5. BlockingQueue<String> queue;
  6. public Producer(BlockingQueue<String> queue) {
  7. this.queue = queue;
  8. }
  9. @Override
  10. public void run() {
  11. try {
  12. String temp = "A Product, 生产线程:"
  13. + Thread.currentThread().getName();
  14. System.out.println("I have made a product:"
  15. + Thread.currentThread().getName());
  16. queue.put(temp);//如果队列是满的话,会阻塞当前线程
  17. } catch (InterruptedException e) {
  18. e.printStackTrace();
  19. }
  20. }
  21. }
  22. 消费者:
  23. import java.util.concurrent.BlockingQueue;
  24. public class Consumer implements Runnable {
  25. BlockingQueue<String> queue;
  26. public Consumer(BlockingQueue<String> queue) {
  27. this.queue = queue;
  28. }
  29. @Override
  30. public void run() {
  31. try {
  32. String temp = queue.take();//如果队列为空,会阻塞当前线程
  33. System.out.println(temp);
  34. } catch (InterruptedException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }
  39. 测试类:
  40. import java.util.concurrent.ArrayBlockingQueue;
  41. import java.util.concurrent.BlockingQueue;
  42. import java.util.concurrent.LinkedBlockingQueue;
  43. public class Test3 {
  44. public static void main(String[] args) {
  45. BlockingQueue<String> queue = new LinkedBlockingQueue<String>(2);
  46. // BlockingQueue<String> queue = new LinkedBlockingQueue<String>();
  47. //不设置的话,LinkedBlockingQueue默认大小为Integer.MAX_VALUE
  48. // BlockingQueue<String> queue = new ArrayBlockingQueue<String>(2);
  49. Consumer consumer = new Consumer(queue);
  50. Producer producer = new Producer(queue);
  51. for (int i = 0; i < 5; i++) {
  52. new Thread(producer, "Producer" + (i + 1)).start();
  53. new Thread(consumer, "Consumer" + (i + 1)).start();
  54. }
  55. }
  56. }
  57. 打印结果:
  58. I have
  59. made a
  60. product:Producer1
  61. I have
  62. made a
  63. product:Producer2
  64. A Product, 生产线程:Producer1
  65. A Product, 生产线程:Producer2
  66. I have
  67. made a
  68. product:Producer3
  69. A Product, 生产线程:Producer3
  70. I have
  71. made a
  72. product:Producer5
  73. I have
  74. made a
  75. product:Producer4
  76. A Product, 生产线程:Producer5
  77. A Product, 生产线程:Producer4

实现方式二阻塞线程:

生产者生产产品阻塞线程,结束后唤醒线程,消费者进行消费。

  1. class Restaurant{
  2. private Object obj;
  3. public void put(){
  4. while (true) {
  5. synchronized (this) {
  6. while (obj != null) {
  7. try {
  8. wait();
  9. } catch (InterruptedException e) {
  10. e.printStackTrace();
  11. }
  12. }
  13. obj = new Object();
  14. System.out.print("Order up! ");
  15. notifyAll();
  16. }
  17. }
  18. }
  19. public void get(){
  20. while (true) {
  21. synchronized (this) {
  22. while (obj == null) {
  23. try {
  24. wait();
  25. } catch (InterruptedException e) {
  26. e.printStackTrace();
  27. }
  28. }
  29. System.out.println("waiter get meal");
  30. obj = null;
  31. notifyAll();
  32. }
  33. }
  34. }
  35. }
  36. class Chef extends Thread{
  37. private Restaurant restaurant;
  38. public Chef(Restaurant restaurant){
  39. this.restaurant = restaurant;
  40. }
  41. @Override
  42. public void run() {
  43. restaurant.put();
  44. }
  45. }
  46. class Waiter extends Thread{
  47. private Restaurant restaurant;
  48. public Waiter(Restaurant restaurant){
  49. this.restaurant = restaurant;
  50. }
  51. @Override
  52. public void run() {
  53. restaurant.get();
  54. }
  55. }
  56. public class Test {
  57. public static void main(String[] args) {
  58. Restaurant restaurant = new Restaurant();
  59. Chef produce = new Chef(restaurant);
  60. Waiter consumer = new Waiter(restaurant);
  61. produce.start();
  62. consumer.start();
  63. }
  64. }

参考:

https://ifeve.com/producers-and-consumers-mode/

https://www.cnblogs.com/renjiaqi/p/11324119.html

http://www.bubuko.com/infodetail-1058947.html

发表评论

表情:
评论列表 (有 0 条评论,257人围观)

还没有评论,来说两句吧...

相关阅读