模拟烧茶的过程

 模拟烧茶的过程:
     1)烧水
     2)需要茶叶的时候发现没茶叶,叫eric去买(没有茶叶,需要买)
     3)需要杯子的时候发现没杯子,叫meten去买(没有杯子,需要买)
     4)放茶叶
     3)倒水

 

public class ExecDemo2 {
 
 public static void main(String[] args){
  
  Tea tea = new Tea();
  tea.setTea(false);
  
  Teacup teacup = new Teacup();
  teacup.setTeacup(false);
  
  Thread burntTea = new BurntTea(tea,teacup,"勾力");
  burntTea.start();
 }

}

/**
 * 共享数据Tea
 * @author Administrator
 *
 */
class Tea{
 
 private boolean isTea;

 public boolean isTea() {
  return isTea;
 }

 public void setTea(boolean isTea) {
  this.isTea = isTea;
 }
 
}
/**
 *  共享数据Teacup
 * @author Administrator
 *
 */
class Teacup{
 
 private boolean isTeacup;

 public boolean isTeacup() {
  return isTeacup;
 }

 public void setTeacup(boolean isTeacup) {
  this.isTeacup = isTeacup;
 }
 
 
}

/**
 * 管理线程 BurntTea
 * @author Administrator
 *
 */
class BurntTea extends Thread {
 
 private Tea tea;
 private Teacup teacup;
 public BurntTea(Tea tea, Teacup teacup,String name){
  
  super(name);
  this.tea = tea;
  this.teacup = teacup;
 }
 
 public void run(){
  
  String name = Thread.currentThread().getName();
  System.out.println(name + "准备烧茶啦");
  System.out.println(name + "烧水啦");
  while(!this.tea.isTea()){
   System.out.println(name + "发现没茶叶啦");
   Runnable buyTea = new BuyTea(tea);
   Thread buyTeaThread = new Thread(buyTea,"eric");
   buyTeaThread.start();
   try {
    buyTeaThread.join();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   if(this.tea.isTea()){
    break;
   }
  }
  
  while(!this.teacup.isTeacup()){
   System.out.println(name + "发现没有茶杯啦");
   Thread buyTeacup = new BuyTeacup(teacup,"meten");
   buyTeacup.start();
   try {
    buyTeacup.join();
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   if(this.teacup.isTeacup()){
    break;
   }
  }
  System.out.println(name + "放茶叶");
  System.out.println(name + "放水");
  try {
   Thread.sleep(2000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  System.out.println("茶烧好啦");
 }
}

/**
 * 买茶杯线程
 * @author Administrator
 *
 */
class BuyTeacup extends Thread{
 
 private Teacup teacup;
 public BuyTeacup(Teacup teacup ,String name){
  
  super(name);
  this.teacup = teacup;
 }
 public void run(){
  
  String name = Thread.currentThread().getName();
  System.out.println(name + "去买茶杯了");
  try {
   Thread.sleep(1000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  System.out.println(name + "买茶杯回来啦");
  
  int a = (int)(Math.random() * 1000) % 3;
  switch(a){
  case 1:
   System.out.println("买茶杯成功");
   this.teacup.setTeacup(true);
   break;
  case 2: 
   System.out.println("买茶杯不成功");
   this.teacup.setTeacup(false);
   break;
  case 3:
   System.out.println("买茶杯成功");
   this.teacup.setTeacup(true);
   break;
  }
  
 }
}

/**
 * 买茶叶线程
 * @author Administrator
 *
 */
class BuyTea implements Runnable{
 
 private Tea tea;
 public BuyTea(Tea tea){
  this.tea = tea;
 }
 
 public void run(){
  
  String name = Thread.currentThread().getName();
  System.out.println(name + "去买茶叶了");
  try {
   Thread.sleep(1000);
  } catch (InterruptedException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  System.out.println(name + "买茶叶回来啦");
  
  int a = (int)(Math.random() * 1000) % 4;
  switch(a){
  case 1:
   System.out.println("买茶叶成功");
   this.tea.setTea(true);
   break;
  case 2 :
   System.out.println("买茶叶不成功");
   this.tea.setTea(false);
   break;
  case 3:
   System.out.println("买茶叶成功");
   this.tea.setTea(true);
   break;
  case 4:
   System.out.println("买茶叶不成功");
   this.tea.setTea(false);
   break;
  }
  
 }
}
 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值