使用计数器解决循环依赖无限循环?

时间:2017-01-05 11:19:34

标签: java loops counter infinite-loop circular-dependency

我在下面有methods以下2 cancelChildTree()

在正确的用例removeChildRelationsIfNecessary()中调用circular dependency,然后继续浏览我的代码。

但是对于我的数据库中的某些数据,父母和孩子之间存在infinite loop。这导致2种方法在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(); } 循环正在发生 - 然后继续寻找特定的父子关系。

方法1:

 private void removeChildRelationsIfNecessary(Parent parent) {
    List<Child> childList = parent.getParentChildRelationship()); 
    for (Child child: childList {
      cancelChildTree(child);
    }
  } 

方法2:

timer = setInterval(function() {
        refreshCurrentTab(streamitem_id);
    }, 5000); refreshCurrentTab(streamitem_id);

1 个答案:

答案 0 :(得分:1)

您似乎在相同的对象上反复调用cancelChildTree(Parent parent)

如果是这样,是什么阻止您使用某个Set<Parent>对象来跟踪给予该方法的父对象?换句话说:而不是使用哑计数器;为什么不跟踪当前处理的那些对象;为了避免再次处理