模拟妈妈做饭,做饭时发现没有盐了,让儿子去买盐(假设买盐需要3分钟),只有盐买回来之后,妈妈才能继续做饭的过程。

解决方案 »

  1.   

    public class Test6 {

    public static void main(String[] args) {
    Thread mother=new Thread(new Mother());
    mother.start();
    }}
    class Home{
    int salt=0;
    }
    class Mother implements Runnable{
    private Home home=new Home();
    public void run() {
    try {
    if(home.salt==0){
    Thread th=new Thread(new Son(home));
    th.start();
    System.out.println("wait..... ");
    Thread.sleep(10000);
    }
    System.out.println("salt is "+home.salt);
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
    class Son implements Runnable{
    private Home home;
    public Son(Home h){
    this.home=h;
    }
    public void run() {
    home.salt=1000;
    }
    }
    只能出现等待的效果,但感觉很不爽。
    那位大侠指教一下
      

  2.   

    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();
    }}
      

  3.   

    使用object.wait(), object.notifyAll()这两个方法就可以了
      

  4.   

    1楼的错误;2楼的细化的很厉害,差不多快整成生产者消费者模型了。不过就楼主的命题来看,根本不需要 wait 和 notify,大致如下:【模拟妈妈做饭】
    做饭时发现没有盐了: if (没盐)
    让儿子去买盐:Thread son = new 儿子(); son.start();
    只有盐买回来之后:son.join(); // 嗯,其实就这样就够了,反正题目也没其它复杂要求
    妈妈才能继续做饭的过程:做饭()
    嗯,其实核心就是:因为没有并发要求,所以 Thread.join() 就够了。
      

  5.   

    join的用法是,子线程运行完之后,父线程执行
      

  6.   

    4楼你这种设计只能说没有表现出楼主需要体现多线程的意义。
    楼主其实就是要生产者、消费者的例子,只不过他表述的太简单了。
    如果按你的join方法,完全可以不用线程,直接if再调用买盐方法岂不更简单?
      

  7.   


    多线程跟join并不冲突,否则join这个函数就被取消了,join函数之所以存在,就是因为它是有价值的。所以它仍然体现了多线程的意义,它只不过没有体现并发的意义。如果楼主真的要生产者消费者的例子,只能说他这个例子举的不好,应该举个厨房里厨师和采购人的例子应该会更合适。
      

  8.   

    写着玩了一下
    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;
    }
    }