程序可以运行,但是不能达到想要的结果,预期的效果是妈妈、小明都有输出,可是代码只有妈妈的输出。
代码如下:public class test2 {
private static final long serialVersionUID = 1L;
Cake cake;
Put put;
Eat eat;
    Thread ming;
Thread mother;

test2(){
cake = new Cake();
put = new Put(cake);
eat = new Eat(cake);
ming = new Thread(eat);
mother = new Thread(put);
ming.start();
mother.start();
}
/**************************************************************/
class Put implements Runnable {// Put类
Cake cake; Put(Cake c) {
cake = c;
} public void run() {

for (int j = 1; j <= 5; j++) {
try {
// 放置饼干花费的时间
Thread.sleep((long) (Math.random() * 10000));
} catch (InterruptedException e) {
}
cake.put(j);

System.out.println("妈妈放了第" + j + "个饼干");
} }
}
/**************************************************************/
class Eat implements Runnable {// Eat类
Cake cake;
int n;
Eat(Cake c) {
cake = c;
} public void run() {

for (int j = 1; j <= 5; j++) {
try {
// 吃掉饼干花费的时间
Thread.sleep((long) (Math.random() * 10000));
} catch (InterruptedException e) {
}
n = cake.eat();
System.out.println("小明吃了第" + n + "个饼干");
}
}
}
/**************************************************************/
class Cake {// Cake类
private int cakenum;
private boolean available = false; public synchronized void put(int cn) {
while (available == true) {
try {
wait();
} catch (InterruptedException e) {
}
cakenum = cn;
available = true;
notify();
}
} public synchronized int eat() {
while (available == false) {
try {
wait();
} catch (InterruptedException e) {
}
}
available = false;
notify();
return cakenum;
} }
public static void main(String[] args) {
test2 t=new test2();
}}

解决方案 »

  1.   

    你把俩 sleep的时间 直接换成1000试试
      

  2.   

    Cake类里面的if判断有问题.public class Test {
    private static final long serialVersionUID = 1L;
    Cake cake;
    Put put;
    Eat eat;
    Thread ming;
    Thread mother; Test() {
    cake = new Cake();
    put = new Put(cake);
    eat = new Eat(cake);
    ming = new Thread(eat);
    mother = new Thread(put);
    ming.start();
    mother.start();
    } /** *********************************************************** */
    class Put implements Runnable {// Put类
    Cake cake; Put(Cake c) {
    cake = c;
    } public void run() { for (int j = 1; j <= 5; j++) {
    try {
    // 放置饼干花费的时间
    Thread.sleep((long) (Math.random() * 100));
    } catch (InterruptedException e) {
    }
    cake.put(j); System.out.println("妈妈放了第" + j + "个饼干");
    } }
    } /** *********************************************************** */
    class Eat implements Runnable {// Eat类
    Cake cake;
    int n; Eat(Cake c) {
    cake = c;
    } public void run() { for (int j = 1; j <= 5; j++) {
    try {
    // 吃掉饼干花费的时间
    Thread.sleep((long) (Math.random() * 100));
    } catch (InterruptedException e) {
    }
    n = cake.eat();
    System.out.println("小明吃了第" + n + "个饼干");
    }
    }
    } /** *********************************************************** */
    class Cake {// Cake类
    private int cakenum;
    private boolean available = false; public synchronized void put(int cn) {
    if (available == false) {
    try {
    wait();
    } catch (InterruptedException e) {
    }
    cakenum = cn;
    available = false;
    notify();
    }
    } public synchronized  int eat() {
    if (available == true) {
    try {
    wait();
    } catch (InterruptedException e) {
    }
    }
    available = false;
    notify();
    return cakenum;
    }
    } public static void main(String[] args) {
    Test t = new Test();
    }}
      

  3.   

    终于有点头绪了,你的问题出在下面红色 } 处public synchronized void put(int cn) {
    while (available == true) {
    try {
    wait();
    } catch (InterruptedException e) {
    }
          }                              cakenum = cn;
    available = true;
    notify();

    }
      

  4.   

     cakenum = cn;
     available = true;
     notify();
    必须在while的外面,否则available的值 一直是false,Motther就不可能把cake放入
    ,小明也就吃不了啦