生产者消费者模型
一、一个生产者 一个消费者
/**步骤:
* 1.生产者线程和消费者线程操作同一个资源
* 脏数据:
* null -- 0.0
* 华为 -- 0.0
*
* 2.两个产品之间来回切换 -- 目的:让步骤1的问题更加明显
* 脏数据:
* null -- 0.0
* 华为 -- 0.0
* 小米 -- 3999.0
* 华为 -- 1999.0
* 解决方法:加锁 -> 生产者线程在生产的过程中(setBrand()和setPrice()),消费者线程不能输出
* 脏数据:
* null -- 0.0
*
* 3.生产一个,消费一个 -> 生产者线程和消费者线程来回切换
*/
public static void main(String[] args) {
Phone phone = new Phone();//null -- 0.0
Producer p1 = new Producer(phone);
Producer p2 = new Producer(phone);
Consumer c1 = new Consumer(phone);
Consumer c2 = new Consumer(phone);
p1.start();
p2.start();
c1.start();
c2.start();
}
产品类--Phone
public class Phone {
private String brand;
private double price;
private boolean isStore;
public Phone() {
}
public Phone(String brand, double price) {
this.brand = brand;
this.price = price;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public boolean isStore() {
return isStore;
}
public void setStore(boolean isStore) {
this.isStore = isStore;
}
}
生产者线程类 -- Producer
//生产者线程
public class Producer extends Thread{
private Phone phone;
public Producer(Phone phone) {
this.phone = phone;
}
private static boolean flag = true;
@Override
public void run() {
while(true){
synchronized (phone) {
if(phone.isStore()){//有库存
try {
phone.wait();//等待:1.将当前线程记录到对象监视器中 2.释放锁资源 3.当前线程进入到阻塞状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(flag){
phone.setBrand("华为");
phone.setPrice(3999);
}else{
phone.setBrand("小米");
phone.setPrice(1999);
}
flag = !flag;
phone.setStore(true);
phone.notify();//唤醒对象监视器中所有的线程
}
}
}
}
消费者线程类 -- Consumer
//消费者线程
public class Consumer extends Thread{
private Phone phone;
public Consumer(Phone phone) {
this.phone = phone;
}
@Override
public void run() {
while(true){
synchronized (phone) {
if(!phone.isStore()){//没有库存
try {
phone.wait();//等待:1.将当前线程记录到对象监视器中 2.释放锁资源 3.当前线程进入到阻塞状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(phone.getBrand() + " -- " + phone.getPrice());
phone.setStore(false);
phone.notify();
}
}
}
}
二、多个生产者 多个消费者
生产者线程类 -- Producer
//生产者线程
public class Producer extends Thread{
private Phone phone;
public Producer(Phone phone) {
this.phone = phone;
}
private static boolean flag = true;
@Override
public void run() {
while(true){
synchronized (phone) {
while(phone.isStore()){//有库存
try {
phone.wait();//等待:1.将当前线程记录到对象监视器中 2.释放锁资源 3.当前线程进入到阻塞状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if(flag){
phone.setBrand("华为");
phone.setPrice(3999);
}else{
phone.setBrand("小米");
phone.setPrice(1999);
}
flag = !flag;
phone.setStore(true);
phone.notifyAll();//唤醒对象监视器中所有的线程
}
}
}
}
消费者线程类 -- Consumer
//消费者线程
public class Consumer extends Thread{
private Phone phone;
public Consumer(Phone phone) {
this.phone = phone;
}
@Override
public void run() {
while(true){
synchronized (phone) {
while(!phone.isStore()){//没有库存
try {
phone.wait();//等待:1.将当前线程记录到对象监视器中 2.释放锁资源 3.当前线程进入到阻塞状态
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(phone.getBrand() + " -- " + phone.getPrice());
phone.setStore(false);
phone.notifyAll();
}
}
}
}