【问题标题】:Solving Circular Dependency infinite loop using counter?使用计数器解决循环依赖无限循环?
【发布时间】: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


    【解决方案1】:

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

    如果是这样,是什么阻止您使用某些Set&lt;Parent&gt; 对象来跟踪提供给该方法的父对象?换句话说:而不是使用哑计数器;为什么不跟踪当前正在处理的那些对象?以避免再次处理它们?

    【讨论】:

    • 我已经更新了我的代码,对不起,这是旧版本,复制并粘贴错误。
    • 我的意思是您一直在 same 对象上调用该方法。当我查看您的编辑时,似乎仍然是那样。因此,我不明白你在这里的评论想要表达什么。
    猜你喜欢
    • 2017-04-27
    • 2020-11-12
    • 2010-11-29
    • 2013-02-04
    • 2010-10-27
    相关资源
    最近更新 更多