程序可以运行,但是不能达到想要的结果,预期的效果是妈妈、小明都有输出,可是代码只有妈妈的输出。
代码如下: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();
}}
代码如下: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();
}}
解决方案 »
- 数组为什么不能初始化?
- 为什么在myeclipse中找不到sun.jdbc.odbc.jdbcodbcdrivername
- 请教!!!!!!有关动态代理中invoke方法。。
- hibernate + spring 拿原生sql返回字段
- jsp中useBean的scope问题
- jetspeed安装问题
- 在Tapestry中实现xlsx文件 导入到数据库Mysql
- 如何用java语言判断网页中是否有视频?
- process finished with exit code -1?
- 我装完weblogic怎么一输入口令就进入了petstore server了?
- strut2 iterator标签实现复杂循环
- HTTP Status 500 怎么解决
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();
}}
while (available == true) {
try {
wait();
} catch (InterruptedException e) {
}
} cakenum = cn;
available = true;
notify();
}
available = true;
notify();
必须在while的外面,否则available的值 一直是false,Motther就不可能把cake放入
,小明也就吃不了啦