生产者消费者模式(问题)
应用场景:生产者消费者问题
- 假设仓库中只能存放一件产品,生产者将生产出来的产品放入仓库,消费者将仓库中产品取走消费。
- 如果仓库中没有产品,则生产者将产品放入仓库,否则停止生产并等待,直到仓库中的产品被消费者取走为止.
- 如果仓库中放有产品,则消费者可以将产品取走消费,否则停止消费并等待,直到仓库中再次放入产品为止。
分析:
这是一个线程同步问题,生产者和消费者共享同一个资源,并且生产者和消费者之间相互依赖,互为条件。
- 对于生产者,没有生产产品之前,要通知消费者等待,而生产了产品之后,又需要马上通知消费者消费
- 对于消费者,在消费之后,要通知生产者已经结束消费,需要生产新的产品以供消费.
- 在生产者消费者问题中,仅有synchronized是不够的
- synchronized 可阻止并发更新同一个共享资源,实现了同步
- synchronized 不能用来实现不同线程之间的消息传递(通信)
Java提供了几个方法解决线程之间的通信问题
方法名 | 作用 |
---|---|
wait() | 表示线程一直在等待,知道其他线程通知,与sleep不同,会释放锁 |
wait(long timeout) | 指定等待的毫秒数 |
notify() | 唤醒一个处于等待状态的线程 |
notifyAll() | 唤醒同一个对象上所有调用wait()方法的线程,优先级别高的线程先调度 |
注意:均是Object类的方法,都只能在同步方法或者同步代码块中使用,否则会抛出异常IllegalMonitorStateException
解决方式一
并发协作模型“生产者/消费者模式”—>管程法
- 生产者:负责生产数据的模块(可能是方法,对象,线程,进程);
- 消费者:负责处理数据的模块(可能是方法,对象,线程,进程);
- 缓冲区:消费者不能直接使用生产者的数据,他们之间有个“缓冲区
生产者将生产好的数据放入缓冲区,消费者从缓冲区拿出数据
//测试:生产者消费者模式 --> 利用缓冲区解决:管程法
//生产者,消费者,产品,缓冲区
public class TestPC {
public static void main(String[] args) {
SynContainer synContainer = new SynContainer();
new Productor(synContainer).start();
new Customer(synContainer, "a").start();
new Customer(synContainer, "b").start();
new Customer(synContainer, "c").start();
}
}
//生产者
class Productor extends Thread{
SynContainer synContainer ;
public Productor(SynContainer synContainer){
this.synContainer = synContainer;
}
@Override
public void run() {
for (int i = 1; i <= 20; i++) {
Chicken chicken = new Chicken(i);
synContainer.push(chicken);
System.out.println("生产者放入了第" + chicken.id + "只鸡");
}
}
}
//消费者
class Customer extends Thread{
SynContainer synContainer ;
public Customer(SynContainer synContainer,String name){
super(name);
this.synContainer = synContainer;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
Chicken chicken = synContainer.pop();
System.out.println("消费者"+Thread.currentThread().getName()+"取出了第" + chicken.id + "只鸡");
}
System.out.println("看看");
}
}
//产品
class Chicken {
int id;//鸡的编号
public Chicken(int id) {
this.id = id;
}
}
//缓冲区
class SynContainer{
//容器的大小
Chicken[] chickens = new Chicken[10];
int count = 0;
//存放
public synchronized void push(Chicken chicken){
//如果满 停止放鸡 等待消费
while (count == chickens.length){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果没有满,丢入鸡
chickens[count] = chicken;
count++;
//通知消费
this.notifyAll();
}
//消费
public synchronized Chicken pop(){
//如果没鸡 等待生产
while (count == 0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//消费鸡
count--;
Chicken chicken = chickens[count];
//通知生产
this.notifyAll();
return chicken;
}
}
解决方式二
并发协作模型“生产者/消费者模式”—>信号灯法
//测试: 生产者消费者模式 --> 信号灯法
//生产者 消费者
public class TestPC2 {
public static void main(String[] args) {
TV tv = new TV();
new Actor(tv).start();
new Watcher(tv).start();
}
}
//演员
class Actor extends Thread{
TV tv;
public Actor(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
if (i % 2 == 0){
this.tv.act("快乐大本营");
}else {
this.tv.act("抖音,记录美好生活");
}
}
}
}
//观众
class Watcher extends Thread{
TV tv;
public Watcher(TV tv){
this.tv = tv;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
this.tv.watch();
}
}
}
class TV{
//演员表演 观众等待 T
//演员等待 观众观看 F
//表演的节目
String voice;
//
boolean flag = true;
//表演
public synchronized void act(String voice){
//如果有节目,演员等待
if (!flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//表演节目
System.out.println("演员表演了节目" + voice);
this.voice = voice;
this.notifyAll();
this.flag = !this.flag;
//通知观众
}
//观看
public synchronized void watch(){
//如果没节目,观众等待
if (flag){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//观看节目
//通知演员表演
this.notifyAll();
System.out.println("观众观看了节目" + voice);
this.flag = !this.flag;
}
}