produceconsumeaction.java

来自「JAVA网络编程电子书及源码」· Java 代码 · 共 48 行

JAVA
48
字号
package multithread;

/**
 * 在这里插入类型说明。
 * 建立日期:(00-7-22 11:14:28)
 * @程序设计者:
 */
public class ProduceConsumeAction {
	private int common_stock;
	private boolean available_state=false;
/**
 * ProduceConsumeAction 构造子注释。
 */
public ProduceConsumeAction() {
	super();
}
/**
 * 在这里插入方法说明。
 * 建立日期:(00-7-22 11:31:16)
 */
public synchronized void consume_action(int who) {
	while (available_state==false){
		try{
			wait();
		}catch (InterruptedException e ){}
	}
	System.out.println("consumer"+who+"#:  "+common_stock);
	available_state=false;
	notify();
}
/**
 * 在这里插入方法说明。
 * 建立日期:(00-7-22 11:21:15)
 */
public synchronized void produce_action(int who,int product) {
	while (available_state==true ){
		try{
			wait();
		}catch (InterruptedException e ){}
	}
	common_stock=product;
	System.out.println(" produer"+who+"#:  "+common_stock);
	available_state=true;
	notify();

}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?