1.概述
由于上一个文章《Java OOD 线程》中的线程是父类主动监听childe,比较耗资源,现改进为childe类醒来后主动联系父类
2.代码
Test.java
class Child implements Runnable { private Dad dad; public Child(Dad dad) {
this.dad = dad;
} public void wakeUp(){
dad.feed(this);
} @Override
public void run() {
try {
Thread.sleep(3000);
} catch (Exception e) {
e.printStackTrace();
}
wakeUp();
}
} class Dad { void feed(Child c) {
System.out.println("feed child");
} } public class Test { public static void main(String[] args) {
Dad d = new Dad();
Child c = new Child(d);
new Thread(c).start();
}
}
3.运行结果