【发布时间】:2017-01-05 11:19:33
【问题描述】:
我在下面有以下 2 个methods。
在正确的用例中 cancelChildTree() 调用 removeChildRelationsIfNecessary() 然后继续执行我的代码。
但是对于我数据库中的一些数据,父母和孩子之间有一个circular dependency。这导致这两种方法在infinite loop 中不断相互调用。
我该如何解决这个问题?我的计划是尝试添加一个 counter 来检测 infinite 循环正在发生 - 然后继续处理特定的父子关系。
方法一:
private void cancelChildTree(Parent parent) {
removeChildRelationsIfNecessary(parent);
//Note that the 2 methods below never get called if the infinite loop occurs
otherMethod();
anotherMethod();
}
方法二:
private void removeChildRelationsIfNecessary(Parent parent) {
List<Child> childList = parent.getParentChildRelationship());
for (Child child: childList {
cancelChildTree(child);
}
}
【问题讨论】:
标签: java loops counter infinite-loop circular-dependency