生产者-消费者(producer-consumer)问题是一个著名的进程同步问题。它描述的是:有一个生产者进程在生产产品,并将这些产品提供给消费者进程去消费。为使生产者进程与消费者进程并发执行,在两者之间设置了一个具有n个缓冲池,生产者进程将它所生产的产品放入一个缓冲区中;消费者进程从一个缓冲区中取走产品去消费。尽管所有的生产者进程和消费者进程都是异步方式运行的,但它们之间必须保持同步,既不允许消费者进程到一个空缓冲区去取东西,也不允许生产者进程向一个已装满产品并且未被取走的缓冲区中投放产品。
视频中的例子,在这里记录下来:
public class ProducerConsumer { public static void main(String[] args) { Basket basket = new Basket(); Producer pp = new Producer(basket); Consumer cc = new Consumer(basket); new Thread(pp).start(); new Thread(cc).start(); } } //食物类 class Foods { int id; Foods(int id) { this.id = id; } //重写toString方法 public String toString() { return "Foods : " + id; } } //放食物的篮子类 class Basket { int num = 0; Foods[] arrFood = new Foods[10]; public synchronized void push(Foods f) { while(num == arrFood.length) { try { this.wait(); }catch(InterruptedException e) { e.printStackTrace(); } } this.notify(); arrFood[num] = f; num++; } public synchronized Foods pop() { while(num == 0) { try { this.wait(); }catch(InterruptedException e) { e.printStackTrace(); } } this.notify(); num--; return arrFood[num]; } } //生产者类 class Producer implements Runnable { Basket basket = null; Producer(Basket basket) { this.basket = basket; } public void run() { for(int i = 0; i < 20; i++) { Foods f = new Foods(i); basket.push(f); System.out.println("生产了: " + f); try { Thread.sleep((int)(Math.random() * 1000)); }catch(InterruptedException e) { e.printStackTrace(); } } } } //消费者类 class Consumer implements Runnable { Basket basket = null; Consumer(Basket basket) { this.basket = basket; } public void run() { for(int i = 0; i < 20; i++) { Foods f = basket.pop(); System.out.println("消费了: " + f); try { Thread.sleep((int)(Math.random() * 1000)); }catch(InterruptedException e) { e.printStackTrace(); } } } }