请你容忍我,因为我仍然在抓捕爪哇人。
下面的示例从一个名为
Parent
在
main
方法。然后它会使该实例进行各种计算。
接下来它会触发一个名为
Child
通过
起源
引用实例
儿童
.
这个
儿童
只是坐在那里监视事情,有时,会触发公共方法
起源
.
它起作用了。问题是,
这种款式不好吗
? 还有别的吗
Java思维方式
做这种工作?
public class Parent {
// main function that fires up the program
public static void main() {
// creates an instance of himself
// and fires go that does all sorts of fuzzy calculus
Parent parent = new Parent();
parent.go();
// creates a new thread of child and starts it
Child child = new Child(parent);
child.start();
}
private void go() {
// all sort of initializations
}
public void getDataFromChild(int data) {
// will get data from running child thread
}
}
public class Child extends Thread {
private Parent parent;
// child constructor grabs Parent instance into "o"
public Child(Parent o) {
parent = o;
}
// this class main loop
public void run() {
while(1==1) {
doSomething();
try {
sleep(1000);
}
catch(Exception e) { }
}
}
private void doSomething() {
parent.getDataFromChild(1);
}
}
谢谢您。