java基础-多线程(7)

112 阅读4分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 10 月更文挑战」的第6天

线程通信

应用场景:生产者和消费者问题

  • 假设仓库中只能存放一件产品,生产者将生产出来的产品放入仓库,消费者将仓库中产品取走消费,
  • 如果仓库中没有产品,则生产者将产品放入仓库,否则停止生产并等待,直到仓库中的产品被消费者取走为止.
  • 如果仓库中放有产品,则消费者可以将产品取走消费,否则停止消费并等待,直到仓库中再次放入产品为止

image-20221004235326498

Java提供了几个方法解决线程之间的通信问题

image-20221004235446557

注意:均是object类的方法,都只能在同步方法或同步代码块中使用,否则会抛出异常llegalMonitorStateException


线程通信-分析

这是一个线程同步问题,生产者和消费者共享同一个资源,并且生产者和消费者之间相互依赖,互为条件。

  • 对于生产者,没有生产产品之前,要通知消费者等待,而生产了产品之后,需要马上通知消费者消费
  • 对于消费者,在消费之后,要通知生产者已经结束消费,需要生产新的产品以供消费.
  • 在生产者消费者问题中,仅有synchronized是不够的
  • synchronized可阻止并发更新同一个共享资源,实现了同步
  • synchronized不能用来实现不同线程之间的消息传递(通信)

线程通信问题解决方式

解决方式1

并发协作模型“生产者/消费者模式”-->管程法

  • 生产者:负责生产数据的模块(可能是方法,对象,线程,进程);
  • 消费者:负责处理数据的模块(可能是方法,对象,线程,进程);
  • 缓冲区:消费者不能直接使用生产者的数据,他们之间有个“缓冲区生产者将生产好的数据放入缓冲区,消费者从缓冲区拿出数据
 /**
  * 测试:生产者消费者模型-->利用缓冲区解决:管程法
  */
 public class Demo33_ThreadPC {
     public static void main(String[] args) {
         SynContainer synContainer = new SynContainer();
         new Producer(synContainer).start();
         new Consumer(synContainer).start();
     }
 }
 ​
 //生产者
 class Producer extends Thread {
     //容缓冲区
     SynContainer container;
 ​
     public Producer(SynContainer container) {
         this.container = container;
     }
 ​
     //生产
     @Override
     public void run() {
         for (int i = 0; i < 100; i++) {
             container.push(new Product(i));
             System.out.println("生产了" + i + "件产品");
         }
     }
 }
 ​
 //消费者
 class Consumer extends Thread {
     //容缓冲区
     SynContainer container;
 ​
     public Consumer(SynContainer container) {
         this.container = container;
     }
 ​
     //消费
     @Override
     public void run() {
         for (int i = 0; i < 100; i++) {
             System.out.println("消费了-->" + container.pop().id + "件产品");
         }
     }
 }
 ​
 //产品
 class Product {
     int id;//产品编号
 ​
     public Product(int id) {
         this.id = id;
     }
 }
 ​
 //缓冲区
 class SynContainer {
     //需要一个容器大小
     Product[] products = new Product[10];
     //容器计数器
     int count = 0;
 ​
     //生产者放入产品
     public synchronized void push(Product product) {
         //如果容器满了,需要等待消费者消费
         /*如果是if的话,假如消费者1消费了最后一个,这是index变成0此时释放锁被消费者2拿到而不是生产者拿到,这时消费者的wait是在if里所以它就直接去消费index-1下标越界,如果是while就会再去判断一下index得值是不是变成0了*/
         while (count == products.length) {
             //通知消费者消费,等待生产
             try {
                 this.wait();
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
         //如果没有满,需要丢入产品
         products[count] = product;
         count++;
         //通知消费者消费
         this.notifyAll();
     }
 ​
     //消费者消费产品
     public synchronized Product pop() {
         //判断是否能消费
         while (count <= 0) {
             //等待生产者生产
             try {
                 this.wait();
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
         //如果可以消费
         count--;
         Product product = products[count];
         //吃完了 通知生产者生产
         this.notifyAll();
         return product;
     }
 }
 ​

解决方式2

并发协作模型“生产者、消费者模式” --> 信号灯法

image-20221005000416874

 /**
  * 测试:生产者消费者模型-->利用缓冲区解决:管程法
  */
 public class Demo33_ThreadPC {
     public static void main(String[] args) {
         SynContainer synContainer = new SynContainer();
         new Producer(synContainer).start();
         new Consumer(synContainer).start();
     }
 }
 ​
 //生产者
 class Producer extends Thread {
     //容缓冲区
     SynContainer container;
 ​
     public Producer(SynContainer container) {
         this.container = container;
     }
 ​
     //生产
     @Override
     public void run() {
         for (int i = 0; i < 100; i++) {
             container.push(new Product(i));
             System.out.println("生产了" + i + "件产品");
         }
     }
 }
 ​
 //消费者
 class Consumer extends Thread {
     //容缓冲区
     SynContainer container;
 ​
     public Consumer(SynContainer container) {
         this.container = container;
     }
 ​
     //消费
     @Override
     public void run() {
         for (int i = 0; i < 100; i++) {
             System.out.println("消费了-->" + container.pop().id + "件产品");
         }
     }
 }
 ​
 //产品
 class Product {
     int id;//产品编号
 ​
     public Product(int id) {
         this.id = id;
     }
 }
 ​
 //缓冲区
 class SynContainer {
     //需要一个容器大小
     Product[] products = new Product[10];
     //容器计数器
     int count = 0;
 ​
     //生产者放入产品
     public synchronized void push(Product product) {
         //如果容器满了,需要等待消费者消费
         /*如果是if的话,假如消费者1消费了最后一个,这是index变成0此时释放锁被消费者2拿到而不是生产者拿到,这时消费者的wait是在if里所以它就直接去消费index-1下标越界,如果是while就会再去判断一下index得值是不是变成0了*/
         while (count == products.length) {
             //通知消费者消费,等待生产
             try {
                 this.wait();
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
         //如果没有满,需要丢入产品
         products[count] = product;
         count++;
         //通知消费者消费
         this.notifyAll();
     }
 ​
     //消费者消费产品
     public synchronized Product pop() {
         //判断是否能消费
         while (count <= 0) {
             //等待生产者生产
             try {
                 this.wait();
             } catch (InterruptedException e) {
                 e.printStackTrace();
             }
         }
         //如果可以消费
         count--;
         Product product = products[count];
         //吃完了 通知生产者生产
         this.notifyAll();
         return product;
     }
 }
 ​

\