I got across a problem, and would like to know if you guys have a formal
way of dealing with it.  Supose we have the following recursive method

public int getDepth(){
        return getParent() == null ? 0 : getParent().getDepth() + 1;
}


If an interface disigner creates an object with "setParent(this)" then
getDepth would cause an infinite loop.  Depending on the method, this
could cause a whole server to go OOME.  Whats the best for to deal whit
this?  I thought of three ways of avoiding it.  What's the best one?

1 - Simply avoid recursion

2 - Something like semaphores

boolean recursing = false;
public int getDepth(){
        if(recursing) throw new RuntimeException("DeadLock");
        recursing = true;
        int ret = 0;
        if(getParent() != null)
          ret = 1 + getParent().getDepth();
        recursing = false;
        return ret;
}

3 - Keep some kinda graph (vertices are conected by getDepth()), something
like:

public void setParent(ThisObject o){
        if(formsCycle(o)) throw new Exception("Cycle detected");
...
}


Maybe I should have sent this message to the user list?  If so, sorry.

Tks
        

-- 
"If we did all the things we are capable of, 
we would literally astound ourselves"
 - Thomas Edison



--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to