Multi-thread communication (producers, consumer issues)

Understand the multi-thread communication, it is to use multiple threads need to have a common shared resources to complete the communication thread through the sharing of resources.
Example: Plant - Consumer - Products
plant products, consumer products, consumer products has become a shared resource between the two
members of the attributes of the product: product name, product prices, the need to produce identification

public class Goods{
	private String name;
	private float price;
	private boolean product;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public float getPrice() {
		return price;
	}
	public void setPrice(float price) {
		this.price = price;
	}
	public boolean isProduct() {
		return product;
	}
	public void setProduct(boolean product) {
		this.product = product;
	}
	public Goods() {
		super();
		// TODO Auto-generated constructor stub
	}
	public Goods(String name, float price, boolean product) {
		super();
		this.name = name;
		this.price = price;
		this.product = product;
	}
	
	
}

Depending on whether plants need to produce identification to produce products
whether judges need to produce 1.
If you need to produce on the modification of the production of identity, wake up the consumer thread, if you do not produce, you go into an infinite wait state, waiting to be awakened

public class Produce implements Runnable {
	private Goods goods = null;

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true) {
			synchronized (goods) {
				
			
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		//需要生产
		if (goods.isProduct()) {
			if (Math.random() > 0.5) {
				goods.setName("擀面皮");
				goods.setPrice(6.0F);
			}else {
				goods.setName("烤羊排");
				goods.setPrice(128);
			}
			//修改标记
			goods.setProduct(false);
			System.out.println("生产者生产了"+goods.getName()+":"+goods.getPrice());
			//唤醒消费者
			goods.notify();
			System.out.println("需要唤醒消费者消费");

		}
		else {
			//生产者进人无限等待状态
			System.out.println("生产者进入无限等待");
			try {
				goods.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
		}
		}
		}
	}

	public Produce(Goods goods) {
		super();
		this.goods = goods;
	}

}

Consumer spending judges that identification does not require the production of consumer representatives, consumer will modify the production of identity, wake producer process, if the need to produce it, the consumer process to enter an infinite waiting, waiting wake

public class Custmor implements Runnable {
	private Goods goods = null;

	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true) {
		synchronized (goods) {
		
			try {
				Thread.sleep(300);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		//判断是否可以进行消费
		if(!goods.isProduct()) {
			//更改生产状态
			goods.setProduct(true);
			System.out.println("消费者消费了"+goods.getName()+"花了"+goods.getPrice());
			//唤醒生产者生产
			System.out.println("唤醒生产者");
			goods.notify();
		}
		else {
			//让消费者进入等待
			System.out.println("消费者进入等待");
			try {
				goods.wait();
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		}
		}
	}

	public Custmor(Goods goods) {
		super();
		this.goods = goods;
	}

}
Released eight original articles · won praise 0 · Views 104

Guess you like

Origin blog.csdn.net/Lu_QQ/article/details/104630816