多线程:一个生产者一个消费者
作者:互联网
一个生产者一个消费者
管程法:消费者、生产者、被消费的对象(包子) 、缓冲区(包子铺)
1.被消费的对象(包子)
package com.mutouren.demo.ex;
/**
* @author wrr
* @date 2021/12/4 0004 14:38
* @description TODO
**/
public class Baozi {
//包子的编号
private int num;
Baozi(int num){
this.num=num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
}
2.缓冲区(包子铺)
package com.mutouren.demo.ex;
/**
* @author wrr
* @date 2021/12/4 0004 14:35
* @description 缓冲区 用来存放包子
**/
public class Hcq {
private Baozi[] baozis = new Baozi[10];
private int count = 0;
/**
* 放包子
**/
public synchronized void pushBaozi(Baozi baozi) {
//如果包子满了 等待消费者消费
if (baozis.length == count) {
//通知消费者消费
try {
System.out.println("生产者等待。。。。。");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("生产者开始放。。。。。");
//如果包子没满 则放包子
baozis[count] = baozi;
count++;
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "生产了第 " + baozi.getNum() + " 个包子");
this.notifyAll();
}
/**
* 吃包子
**/
public synchronized void eatBaozi() {
//如果没有包子 等待生产者生产包子
if (count == 0) {
//通知消费者消费
try {
System.out.println("消费者等待。。。。");
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
//如果有 则进行消费
System.out.println("消费者开始吃。。。。");
count--;
Baozi baozi = baozis[count];
// System.out.println("count = " + count);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(Thread.currentThread().getName() + "消费了第 " + baozi.getNum() + " 个包子");
this.notifyAll();
}
}
3.生产者
package com.mutouren.demo.ex;
/**
* @author wrr
* @date 2021/12/4 0004 14:37
* @description 生产者
**/
public class Scz implements Runnable {
//生产者消费者都要从缓冲区拿包子
private Hcq hcq;
Scz(Hcq hcq){
this.hcq=hcq;
}
@Override
public void run() {
for (int i = 1; i <= 100; i++) {
hcq.pushBaozi(new Baozi(i));
}
}
}
4.消费者
package com.mutouren.demo.ex;
/**
* @author wrr
* @date 2021/12/4 0004 14:37
* @description 消费者
**/
public class Xfz implements Runnable {
private Hcq hcq;
public Xfz(Hcq hcq) {
this.hcq=hcq;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
hcq.eatBaozi();
}
}
}
5.主类
package com.mutouren.demo.ex;
/**
* @author wrr
* @date 2021/12/4 0004 14:58
* @description TODO
**/
public class Test {
public static void main(String[] args) {
Hcq hcq = new Hcq();
Scz scz = new Scz(hcq);
Xfz xfz = new Xfz(hcq);
new Thread(xfz,"消费者1").start();
new Thread(scz,"生产者1111").start();
}
}
结果:
总结:
1.黄色的框说明 wait会释放锁并且在再次得到锁之后 会继续从原来的地方执行,而不是从头开始
2.结果说明了 是先生产后消费的
标签:count,hcq,消费者,生产者,public,num,println,多线程,包子 来源: https://blog.csdn.net/Maspue/article/details/121719151