消费者和生产者模型(两个例子)

/**
 * 产品
 */
public class Product {

    String name;
    String color;
    boolean flag = false;//没有产品
    /**
     * 进行消费
     */
    public synchronized void get() {
        //没产品,等待生产
        if(flag==false) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //有产品进行消费
        System.out.println("消费了:"+color+"的"+name);
        //消费完,没有产品
        flag = false;
        //通知生产
        notify();
    }
    /**
     * 进行生产
     * @param name 产品名称
     * @param color 产品颜色
     */
    public synchronized void put(String name,String color) {
        //有产品,等待消费
        if(flag==true) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        //把传进来的属性进行赋值操作
        this.name = name;
        this.color = color;
        //没产品进行生产
        System.out.println("生产了:"+color+"的"+name);
        //有产品了
        flag = true;
        //通知消费
        notify();
    }

}


/**
 *消费者负责消费产品
 */
public class Consumer implements Runnable {
    Product p;

    public Consumer() {

    }

    public Consumer(Product p) {
        this.p = p;
    }

    @Override
    public void run() {
        
        while (true) {
            p.get();
        }
    }

}


/**
 * 生产者负责生产产品
 */
public class Producer implements Runnable {
    Product p;

    public Producer() {

    }

    public Producer(Product p) {
        this.p = p;
    }

    @Override
    public void run() {
        int i = 0;
        while (true) {
            if (i % 2 == 0) {
                p.put("馒头", "白色");
            } else {
                p.put("玉米饼", "黄色");
            }
            i++;
        }
    }

}

/**
 * 测试类
 */
public class Test {

    public static void main(String[] args) {
        Product p = new Product();
        new Thread(new Producer(p)).start();
        new Thread(new Consumer(p)).start();
    }
}

//第二个例子

import java.util.List;
import java.util.Vector;

/**
 * 资源类(包子)
 */
public class Bun {

    List<Integer> bz = new Vector<Integer>();
    
    /**
     * 买包子的方法
     * @throws InterruptedException
     */
    public synchronized void buy() throws InterruptedException {
        Thread.sleep(100);
        //包子卖空了,等待做包子再买
        if(bz.size()==0) {
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.out.println("消费者买到第"+bz.size()+"个包子");
        //买到1个就少1个
        bz.remove(bz.size()-1);
        //通知生产者做包子
        notify();
    }
    /**
     * 制作包子的方法
     * @throws InterruptedException
     */
    public synchronized void make() throws InterruptedException {
        Thread.sleep(100);
        //包子做到10个了,先不做,开始卖
        if(bz.size()>=10) {
            try {
                wait();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        System.out.println("生产者在做第"+(bz.size()+1)+"个包子");
        //制作一个就多一个
        bz.add(666);
        //通知消费者买包子
        notify();
    }
}


**
 * 消费者负责消费产品
 */
public class Consumer2 implements Runnable {
    Bun b;

    public Consumer2() {

    }

    public Consumer2(Bun b) {
        this.b = b;
    }

    @Override
    public void run() {
        while (true) {
            try {
                b.buy();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}


/**
 * 生产者负责生产产品
 */
public class Producer2 implements Runnable {
    Bun b;

    public Producer2() {

    }

    public Producer2(Bun b) {
        this.b = b;
    }

    @Override
    public void run() {
        while (true) {
            try {
                b.make();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}


/**
 *测试类
 */
public class Test2 {

    public static void main(String[] args) {
        Bun b = new Bun();
        new Thread(new Producer2(b)).start();
        new Thread(new Consumer2(b)).start();
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值