这是How can an instance communicate with its instantiator?
的后续问题如果我有以下内容,并且方法signal()被B的2个实例同时调用,这将导致每个B调用reset()两次冲突。我该怎么做才能解决它?
如果发生的事件仅在所有B完成重置后才会重置,则可以重置。
public class A {
private B b[];
public A() {
//for loop
b[i] = new B(this);
}
public void signal() {
//tells every B to reset.
//for loop
b[i].reset();
}
}
public class B {
A creator;
public B(A creator) {
this.creator = creator;
}
public void reset() {
//very critical operation that should not happen back to back
}
public void main(String[] args) {
while(true)
if(isBad()==true) {
//Something bad happen. Tell A to reset everyone.
creator.signal();
}
}
}