日期:2014-05-20  浏览次数:20658 次

Java生产者消费者模型代码
有人能提供一下简单的Java生产者消费者模型代码吗?
------解决方案--------------------
package day09;

public class Store {
private int count;

private final int MAX_SIZE;

public Store(int size) {
MAX_SIZE=size;
}

public synchronized void addData(){


while(count==MAX_SIZE){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

count++;
this.notifyAll();
System.out.println(Thread.currentThread().getName()+" addData:"+count);
}

public synchronized void removeData(){
while(count==0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

System.out.println(Thread.currentThread().getName()+" remove Data:"+count);
count--;
this.notifyAll();

}

}