利用线程等待机制模仿买冰淇淋案例

package cn.dali3.code07;
/*我们模拟一个事件
* 小A去冷饮厅吃甜筒和圣代。
*
* 过程:小A告诉老板,我要吃冷饮。--->老板开始做,冰淇淋和圣代随机。做好了告诉你小A,做好了-->小A开始吃,吃完了接着要-->........
* 分析:
*   冰淇淋:看做是一个类,属性有圣代和甜筒,还有一个状态属性,有或者没有两种状态。可以使用bool
*   小A:是一个线程。先告诉老板要吃冷饮,唤醒老板,进入wait状态,等待老板做好唤醒
*   老板:判断冰淇淋有没有,如果没有了,开始做,如果有进入wait等待小A唤醒
*   注:这里要保证老板和小A两个线程不同时进行,也就是要保证冷饮只能在一个人手里
*   要么在老板手里,要么在小A手里,所以我们要使用同步代码快,对象锁就可以使用Ice类的对象*/

public class Demo01 {
    public static void main(String[] args) {
        IceHall iH = new IceHall();
        Person a = new Person(iH.getIce());
        a.start();
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        iH.start();
    }
}

Ice类:

package cn.dali3.code07;

public class Ice {
    private String kind;
    boolean state=false;

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public boolean isState() {
        return state;
    }

    public void setState(boolean state) {
        this.state = state;
    }
}

IceHall类:

package cn.dali3.code07;

public class IceHall extends Thread {
    private Ice ice = new Ice();
    private int count = 0;

    public Ice getIce() {
        return ice;
    }

    public void setIce(Ice ice) {
        this.ice = ice;
    }

    @Override
    public void run() {
       while(true){
           synchronized (ice){
               if (ice.state == true) {//如果还有冰淇淋则进入休眠状态
                   try {
                       ice.wait();
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
               } else {
                   if (count % 2 == 0)//所及分配属性
                       ice.setKind("甜筒");
                   else
                       ice.setKind("圣代");
                   System.out.println("正在做"+ice.getKind());
                   count++;
                   try {
                       Thread.sleep(5000);//等待五秒
                   } catch (InterruptedException e) {
                       e.printStackTrace();
                   }
                   System.out.println(ice.getKind()+"做好了");
                   ice.state=true;
                   ice.notify();
               }
           }
       }
    }
}

person类:

package cn.dali3.code07;

public class Person extends Thread {
    private Ice ice = new Ice();

    public Person(Ice ice) {
        this.ice = ice;
    }

    public Ice getIce() {
        return ice;
    }

    public void setIce(Ice ice) {
        this.ice = ice;
    }

    @Override
    public void run() {
        while(true){
            synchronized (ice){
                if(ice.state==false){
                    System.out.println("老板,要一个冰淇淋,甜筒圣代看着给");
                    try {
                        ice.notify();
                        ice.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }else{
                    System.out.println("我要开始吃"+ice.getKind());
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    ice.setState(false);
                }
            }
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值