一、使用wait、notify实现
import java.util.*;
public class test {
public static void main(String[] args) {
AbstractStorage abstractStorage=new Storage();
Producer p1=new Producer(abstractStorage);
Producer p2=new Producer(abstractStorage);
Producer p3=new Producer(abstractStorage);
Producer p4=new Producer(abstractStorage);
Producer p5=new Producer(abstractStorage);
Producer p6=new Producer(abstractStorage);
Consumer c1=new Consumer(abstractStorage);
Consumer c2=new Consumer(abstractStorage);
Consumer c3=new Consumer(abstractStorage);
p1.setNum(10);
p2.setNum(10);
p3.setNum(10);
p4.setNum(10);
p5.setNum(10);
p6.setNum(10);
c1.setNum(50);
c2.setNum(20);
c3.setNum(30);
c1.start();
c2.start();
c3.start();
p1.start();
p2.start();
p3.start();
p4.start();
p5.start();
p6.start();
}
}
interface AbstractStorage{
void consume(int num);
void produce(int num);
}
class Storage implements AbstractStorage{
private final int MAX_SIZE=100;
private LinkedList list=new LinkedList();
public void produce(int num){
synchronized (list){
while(list.size()+num>MAX_SIZE){
System.out.println("【要生产的产品数量】:" + num + "\t【库存量】:" + list.size() + "\t暂时不能执行生产任务!");
try {
list.wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
for(int i=0;i<num;i++){
list.add(new Object());
}
System.out.println("【已经生产产品数】:" + num + "\t【现仓储量为】:" + list.size());
list.notifyAll();
}
}
public void consume(int num){
synchronized (list){
while (num>list.size()){
System.out.println("【要消费的产品数量】:" + num + "\t【库存量】:" + list.size() + "\t暂时不能执行生产任务!");
try{
list.wait();
}catch (InterruptedException e){
e.printStackTrace();
}
}
for(int i=0;i<num;i++){
list.remove();
}
System.out.println("【已经消费产品数】:" + num + "\t【现仓储量为】:" + list.size());
list.notifyAll();
}
}
}
class Producer extends Thread{
private int num;
public AbstractStorage abstractStorage;
public Producer(AbstractStorage abstractStorage){
this.abstractStorage=abstractStorage;
}
public void setNum(int num){
this.num=num;
}
@Override
public void run(){
produce(num);
}
public void produce(int num){
abstractStorage.produce(num);
}
}
class Consumer extends Thread{
private int num;
private AbstractStorage abstractStorage;
public Consumer(AbstractStorage abstractStorage){
this.abstractStorage=abstractStorage;
}
public void setNum(int num){
this.num=num;
}
@Override
public void run(){
consume(num);
}
public void consume(int num){
abstractStorage.consume(num);
}
}
输出结果
【要消费的产品数量】:50 【库存量】:0 暂时不能执行生产任务!
【已经生产产品数】:10 【现仓储量为】:10
【要消费的产品数量】:50 【库存量】:10 暂时不能执行生产任务!
【已经生产产品数】:10 【现仓储量为】:20
【已经生产产品数】:10 【现仓储量为】:30
【已经生产产品数】:10 【现仓储量为】:40
【已经消费产品数】:30 【现仓储量为】:10
【要消费的产品数量】:20 【库存量】:10 暂时不能执行生产任务!
【已经生产产品数】:10 【现仓储量为】:20
【已经生产产品数】:10 【现仓储量为】:30
【要消费的产品数量】:50 【库存量】:30 暂时不能执行生产任务!
【已经消费产品数】:20 【现仓储量为】:10
【要消费的产品数量】:50 【库存量】:10 暂时不能执行生产任务!
wait、notify、notifyAll详解