当前位置: 代码迷 >> J2SE >> java多线程模拟一上场景
  详细解决方案

java多线程模拟一上场景

热度:4431   发布时间:2013-02-25 00:00:00.0
java多线程模拟一下场景
模拟妈妈做饭,做饭时发现没有盐了,让儿子去买盐(假设买盐需要3分钟),只有盐买回来之后,妈妈才能继续做饭的过程。






------解决方案--------------------------------------------------------
Java code
public class Test {    static class Mother extends Thread{        /** 盐的用量 */        final int SaltUseage = 40;        volatile boolean cooking = true;        Home home;        public void run(){            try {                while(cooking){                    Salt salt = home.salt;                    if(salt.value<SaltUseage){                        home.son.buySalt();                        waiting();                    }                    System.out.println("[妈妈]盐  ==>> 当前量:"+salt.value+" 用去量:"+SaltUseage+" 剩余量:"+(salt.value-SaltUseage));                    salt.value -= SaltUseage;                    otherThings();                }            } catch (Exception e) {                cooking = false;            }        }        private void otherThings() {            try {                sleep(1000);            } catch (Exception e) {                cooking = false;            }        }        private synchronized void waiting() throws InterruptedException {            System.out.println("[妈妈]盐  ==>> 当前量:"+home.salt.value+"  等待儿子去买盐。");            home.mother.wait();        }        private synchronized void notice(){            home.mother.notify();        }    }    static class Son extends Thread{        /** 盐的购买量 */        final int SaltPurchases = 60;        volatile boolean free = true;        Home home;        public void run(){            try {                while(free){                    waiting();                    sleep(2000);                    System.out.println("[儿子]盐  ==>> 当前量:"+home.salt.value+" 购买量:"+SaltPurchases+" 剩余量:"+(home.salt.value+SaltPurchases));                    home.salt.value += SaltPurchases;                    notice();                }            } catch (Exception e) {                free = false;            }        }        public synchronized void buySalt(){            notify();        }        private void notice() {            System.out.println("[儿子]盐  ==>> 当前量:"+home.salt.value+" 告知妈妈盐已买到。");            home.mother.notice();        }        private synchronized void waiting() throws InterruptedException {            System.out.println("[儿子]盐  ==>> 当前量:"+home.salt.value+" 等待下次购买。");            wait();        }    }    static class Salt{        int value = 90;    }    static class Home{        Mother mother = new Mother();        Son son = new Son();        Salt salt = new Salt();        public Home(){            mother.home = this;            son.home = this;        }        public void startCooking(){            mother.start();            son.start();        }        public void stopCooking(){            mother.cooking=false;            son.free=false;            mother.interrupt();            son.interrupt();        }    }        /**     * 测试用例     */    public static void main(String[] args) throws Exception {        Home home = new Home();        home.startCooking();        Thread.sleep(15000);        home.stopCooking();    }}
------解决方案--------------------------------------------------------
写着玩了一下
Java code
public class Mouther extends Thread{    private Time t;    public Mouther(Time t){        this.t=t;    }    public void run(){            synchronized (t) {            System.out.println("做饭..");            if(t.isHaveSalt()==false){                try {                    t.wait();                } catch (InterruptedException e) {                    // TODO Auto-generated catch block                    e.printStackTrace();                }                System.out.println("有盐做饭了");                System.exit(1);            }    }}}public class Son extends Thread{    private Time t;    public Son(Time t){        this.t=t;    }    public void run(){                synchronized (t) {            System.out.println("sonrun");            if(t.isHaveSalt()==false){                System.out.println("买盐");                for(int i=this.t.getTime();i>=0;i--){                    System.out.println("时间:"+i);                }                System.out.println(t.getTime());                System.out.println("买完.....");                t.notifyAll();            }        }    }    public static void main(String[] args) {            Time t= new Time();        t.setTime(10000);        t.setHaveSalt(false);        Son s = new Son(t);        Mouther m = new Mouther(t);        m.start();        s.start();            }}public class Time {    private int time;        private boolean haveSalt;    public boolean isHaveSalt() {        return haveSalt;    }    public void setHaveSalt(boolean haveSalt) {        this.haveSalt = haveSalt;    }    public int getTime() {        return time;    }    public void setTime(int time) {        this.time = time;    }}
  相关解决方案