我有以下课程.
public class B
{
public A a;
public B()
{
a= new A();
System.out.println("Creating B");
}
}
Run Code Online (Sandbox Code Playgroud)
和
public class A
{
public B b;
public A()
{
b = new B();
System.out.println("Creating A");
}
public static void main(String[] args)
{
A a = new A();
}
}
Run Code Online (Sandbox Code Playgroud)
可以清楚地看到,类之间存在循环依赖关系.如果我试着跑A级,我最终会得到一个StackOverflowError.
如果创建了依赖关系图,其中节点是类,则可以轻松识别此依赖关系(至少对于具有少量节点的图).那为什么JVM不能识别这个,至少在运行时?StackOverflowErrorJVM可以在开始执行之前至少发出警告,而不是抛出.
[更新]某些语言不能具有循环依赖关系,因为这样就不会构建源代码.例如,请参阅此问题和接受的答案.如果循环依赖是C#的设计气味那么为什么它不适用于Java呢?只是因为Java可以(编译循环依赖的代码)?
[update2]最近发现了jCarder.根据该网站,它通过动态检测Java字节代码并在对象图中查找周期来发现潜在的死锁.任何人都可以解释该工具如何找到周期?
public class Category {
private Category parentCategory;
private Set<Category> childCategories;
private String name;
public Category() {
childCategories = new HashSet<Category>();
}
public Category getParentCategory() {
return parentCategory;
}
public void setParentCategory(Category parentCategory) {
this.parentCategory = parentCategory;
}
public Set<Category> getChildCategories() {
return childCategories;
}
public void setChildCategories(Set<Category> childCategories) {
this.childCategories = childCategories;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Category [childCategories=" + childCategories + …Run Code Online (Sandbox Code Playgroud)