OOA&OOD复习笔记...

2012-11-06  付民 

package ad;

class Child implements Runnable{
private boolean wakenUp = false;
void wakeUp(){
wakenUp = true;
}

public boolean isWakenUp() {
// TODO Auto-generated method stub
return false;
}
public void run(){
try{
Thread.sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}
this.wakeUp();
}
}
class Dad implements Runnable {
Child c;
public Dad(Child c){
this.c = c;
}
public void run(){
while(!c.isWakenUp()){
try{
Thread.sleep(1000);
}catch(InterruptedException e){
e.printStackTrace();
}
}
feed(c);
}
private void feed(Child c) {
// TODO Auto-generated method stub
System.out.println("feed Child");
}
}

public class Test {
public static void main(String[] args){
Child d = new Child();
new Thread(d).start(); //启动线程
new Thread(new Dad(d)).start();//启动线程
}

}

307°/3072 人阅读/0 条评论 发表评论

登录 后发表评论