多线程-生产者消费者模式
package com.liujinghe.a3;
public class Goods {
/**
* 商品类,用于将生产者和消费者联系起来
*/
private String name;
private int price;
private boolean shouldProduct;
public Goods() {
// TODO Auto-generated constructor stub
}
public Goods(String name, int price, boolean shouldProduct) {
super();
this.name = name;
this.price = price;
this.shouldProduct = shouldProduct;
}
@Override
public String toString() {
return "Goods [name=" + name + ", price=" + price + ", shouldProduct=" + shouldProduct + "]";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPrice() {
return price;
}
public void setPrice(int price) {
this.price = price;
}
public boolean isShouldProduct() {
return shouldProduct;
}
public void setShouldProduct(boolean shouldProduct) {
this.shouldProduct = shouldProduct;
}
}
package com.liujinghe.a3;
/**
* 消费者线程
* 消费者如果需要消费
* 进行消费
* 更改状态
* 唤醒生产者
* 消费者如果不需要消费
* 进入阻塞状态
* 等待生产者唤醒
* @author Administrator
*
*/
public class Customer implements Runnable{
private Goods goods;
public Customer(Goods goods) {
// TODO Auto-generated constructor stub
this.goods = goods;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
//加锁
synchronized(goods) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//如需需要消费
if(!goods.isShouldProduct()) {
System.out.println("消费者开始消费,购买了:"+goods.getName()+",价格:"+goods.getPrice());
//更改状态
goods.setShouldProduct(true);
//唤醒生产者
goods.notify();
}else {
//不需要消费
System.out.println("消费者进入阻塞状态,等待生产者唤醒");
try {
goods.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
package com.liujinghe.a3;
/**
* 生产者线程
* 如果需要生产,给属性赋值,
* 更改状态
* 唤醒消费者
* 如果不需要生产,进入阻塞状态
* @author Administrator
*
*/
public class Productor implements Runnable{
private Goods goods;
public Productor(Goods goods) {
// TODO Auto-generated constructor stub
this.goods = goods;
}
@Override
public void run() {
// TODO Auto-generated method stub
while(true) {
//上锁
synchronized(goods) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
int count=0;//计数器
if(goods.isShouldProduct()) {
//如果需要生产,开始生产
System.out.println("生产者生产中........");
if(count%2==0) {
goods.setName("法拉第");
goods.setPrice(32948);
}else {
goods.setName("劳斯莱斯");
goods.setPrice(2324);
}
System.out.println("生产者生产了:"+goods.getName()+",价格"+goods.getPrice());
//改变状态
goods.setShouldProduct(false);
count++;
//唤醒消费者
goods.notify();
}else {
//进入阻塞状态
System.out.println("生产者开始等待消费者唤醒");
try {
goods.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
}
package com.liujinghe.a3;
public class Demo {
public static void main(String[] args) {
Goods goods = new Goods("莱斯基尼",3244,false);
Thread thread1 = new Thread(new Productor(goods));
Thread thread2 = new Thread(new Customer(goods));
thread1.start();
thread2.start();
}
}