浏览 2869 次
锁定老帖子 主题:模拟烧茶的过程
该帖已经被评为新手帖
|
|
---|---|
作者 | 正文 |
发表时间:2011-07-25
模拟烧茶的过程: 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; } } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2011-07-26
还用设计模式哦
|
|
返回顶楼 | |
发表时间:2011-07-26
反正都是顺序运行!为什么要用多线程??直接顺序运行不是一样么!?
|
|
返回顶楼 | |
发表时间:2011-07-26
水都烧了茶叶还没买
|
|
返回顶楼 | |
发表时间:2011-07-26
没时间看代码
|
|
返回顶楼 | |
发表时间:2011-07-27
this.tea.isTea()
这个茶叶难道可能不是茶叶吗? BuyTea 从对象命名有点奇怪,buyTea命名是一个动词, 怎么能作为类名呢? Tea 既然是eric 买,Eric 应该有buy tea的方法,这里没看到。 |
|
返回顶楼 | |
发表时间:2011-07-27
while(!this.tea.isTea())
很耗CPU 你可以在buy tea方法中加一个回调函数,也就是用到观察者模式 tea 如果买好就调用回调函数去加茶叶 买茶杯也一样 |
|
返回顶楼 | |
发表时间:2011-07-27
可以增加一个Buyer对象, Buyer对象中加一个buy(String TypeOfGoods) 方法 这样你的系统就可以买更多的东西了,可以买些小茶点了 eric,meten 定义成Buyer的一个对象 |
|
返回顶楼 | |