学习博客:卖包子案例封装改进版

没啥说的就是上一次的包子铺案例加入了封装,还把方法简化了下。

package cn.zsp.thread_05;

//wait() ; / /使用了wait就等待了。立即释放锁。将来醒过来的时候,是从这里醒过来的时候
public class BaoZiDemo {
    public static void main(String[] args) {
    BaoZiType baozi= new BaoZiType();
    ConsumeBaozi1 b1= new ConsumeBaozi1(baozi);
    ConsumeBaozi2 b2= new ConsumeBaozi2(baozi);
    ProduceBaoZi b3=new ProduceBaoZi(baozi);
    Thread prob=new Thread(b3);
    Thread baozi1 =new Thread(b1);
    Thread baozi2 =new Thread(b2);
    prob.start();
    baozi1.start();
    baozi2.start();

    }
}

package cn.zsp.thread_05;

public class BaoZiType {
      private String baozi1="肉包";
      private String baozi2="菜包";
      private int number1=0;
      private int number2=0;
      private boolean flag;

      public synchronized void consume1(){
      if(number1<1 )
      {
            try {
                  this.wait();
            } catch (InterruptedException e) {
                  e.printStackTrace();
            }
      }
            System.out.println("肉包子卖出去了一个");
            number1--;
            if(number1<5)
            this.notify();

      }
      public synchronized void consume2(){

            if(number2<1 )
            {
                  try {
                        this.wait();
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
            }
            System.out.println("菜包子卖出去了一个");
            number2--;
            if(number2<5)
            this.notify();

      }
      public synchronized void product(){
            if(number1>5  ||number2>5)
            {
                  try {
                        this.wait();
                  } catch (InterruptedException e) {
                        e.printStackTrace();
                  }
            }

            System.out.println("现在有"+number1+"个肉包子"+number2+"个菜包子");
            number1+=3;
            number2+=3;

            this.notify();
      }


}

package cn.zsp.thread_05;

public class ConsumeBaozi1 implements Runnable{
BaoZiType b=new BaoZiType();

public ConsumeBaozi1() {
}

public ConsumeBaozi1(BaoZiType b) {
    this.b = b;
}
@Override
public void run() {

        while(true) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            b.consume1();
        }
}

}

package cn.zsp.thread_05;

public class ConsumeBaozi2 implements Runnable {
    BaoZiType b = new BaoZiType();

    public ConsumeBaozi2() {
    }

    public ConsumeBaozi2(BaoZiType b) {
        this.b = b;
    }

    public void run() {

        while (true) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            b.consume2();
        }
    }
}
package cn.zsp.thread_05;

public class ProduceBaoZi implements  Runnable {
    BaoZiType b = new BaoZiType();

    public ProduceBaoZi() {
    }

    public ProduceBaoZi(BaoZiType b) {
        this.b = b;
    }


    @Override
    public void run() {

        while (true) {
            try {
                Thread.sleep(200);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        b.product();
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值